98

Will Java code built and compiled against a 32-bit JDK into 32-bit byte code work in a 64-bit JVM? Or does a 64-bit JVM require 64-bit byte code?

To give a little more detail, I have code that was working in a Solaris environment running a 32-bit JVM, but now I'm getting issues after upgrading the JDK and Weblogic Server to 64-bit.

3
  • 3
    please clarify "issues". Apr 24, 2009 at 8:15
  • I'm having a similar issue - deploying a spring app on 64 bit weblogic server. We get various class not found exceptions, and other unhelpful errors. Furthermore, it deploys and runs on some 64 bit machines, but not others. We can't tell what's different however. Did you solve this?
    – nont
    Sep 15, 2009 at 18:22
  • 2
    @nont - whatever the problem is, it is not 32vs64 bit compilation.
    – Stephen C
    Apr 22, 2013 at 7:35

9 Answers 9

95

Yes, Java bytecode (and source code) is platform independent, assuming you use platform independent libraries. 32 vs. 64 bit shouldn't matter.

4
  • I ran into this while searching for a question i had. SO i ran my application under a 32 bit JVM and used 64 bit native library. It ran fine. But when i run my application under a 64 bit JVM and use 32 bit native library, it fails. How could this be possible? Just curious. Jun 9, 2013 at 12:43
  • 7
    @umangdesai native libraries are not platform independent libraries, hence the assumption doesn't hold. Jul 10, 2013 at 3:27
  • Does "shouldn't matter" mean that code compiled with 32-bit javac will take advantage of the memory made available with 64-bit java ? Oct 15, 2015 at 17:34
  • 1
    If you're getting stung on this, watch out for native libraries that have been bundled into a jar that work for one platform, but not for the one giving you issues. (If you have no idea what I'm referring to, see things like this: stackoverflow.com/a/14051512/155631 ).
    – Matt S.
    Sep 16, 2016 at 21:59
21

I accidentally ran our (largeish) application on a 64bit VM rather than a 32bit VM and didn't notice until some external libraries (called by JNI) started failing.

Data serialized on a 32bit platform was read in on the 64bit platform with no issues at all.

What sort of issues are you getting? Do some things work and not others? Have you tried attaching JConsole etc and have a peak around?

If you have a very big VM you may find that GC issues in 64 bit can affect you.

5
  • 1
    are you saying that JNI libraries won't work in a 64 bit VM if they're 32 bit?
    – C. Ross
    May 25, 2010 at 20:27
  • 1
    They don't work. A colleague had reported that they did (which I found suspicious - to say the least). I wondered if he was running on Solaris and that there was some sort of thunking going on. There wasn't; he was mistaken and it was running under 32bit. Jun 28, 2010 at 21:45
  • I have had a similar issue with a JNI library. There was no compatability between the 32-bit and 64-bit libraries. Dec 8, 2010 at 17:18
  • Indeed, your JNI libs need to be replaced. You can probably just download the 64bit alternative from the vendor's website. (That did the trick for me, for all JNI libs that I used).
    – bvdb
    Dec 3, 2014 at 14:42
  • These were internal libraries and no 64 bit equivalent was available, so back to 32 bit was the order of the day.. Dec 3, 2014 at 22:05
11

Yes to the first question and no to the second question; it's a virtual machine. Your problems are probably related to unspecified changes in library implementation between versions. Although it could be, say, a race condition.

There are some hoops the VM has to go through. Notably references are treated in class files as if they took the same space as ints on the stack. double and long take up two reference slots. For instance fields, there's some rearrangement the VM usually goes through anyway. This is all done (relatively) transparently.

Also some 64-bit JVMs use "compressed oops". Because data is aligned to around every 8 or 16 bytes, three or four bits of the address are useless (although a "mark" bit may be stolen for some algorithms). This allows 32-bit address data (therefore using half as much bandwidth, and therefore faster) to use heap sizes of 35- or 36-bits on a 64-bit platform.

5
  • 3
    You surprise me. I didn't think there was such a thing as 32-bit byte code or 64-bit byte code.
    – Jon Skeet
    Apr 23, 2009 at 21:57
  • 3
    Rereading your answer - are you sure you didn't just mean it the other way round? (Yes then no.)
    – Jon Skeet
    Apr 23, 2009 at 21:57
  • +1 to Jon Skeet. I was writing the same comment but got called away.
    – Michael Myers
    Apr 23, 2009 at 22:01
  • I meant no then yes, but with the questions the other way around. Have rolled back an edit and edited (and put a bit more information in). Apr 24, 2009 at 7:15
  • 5
    @Jon Skeet: there is no 32-bit and 64-bit bytecode, but when JITed the pointers in the JVM are (usually) 32 or 64 bit, depending on the platform. And with Compressed OOPS they can use 32bit pointers in many places, even on 64bit JVMs. That saves quite a bit of memory and increases code locality, thus leading to greater speed. Apr 24, 2009 at 8:20
10

All byte code is 8-bit based. (That's why its called BYTE code) All the instructions are a multiple of 8-bits in size. We develop on 32-bit machines and run our servers with 64-bit JVM.

Could you give some detail of the problem you are facing? Then we might have a chance of helping you. Otherwise we would just be guessing what the problem is you are having.

0
8

Unless you have native code (machine code compiled for a specific arcitechture) your code will run equally well in a 32-bit and 64-bit JVM.

Note, however, that due to the larger adresses (32-bit is 4 bytes, 64-bit is 8 bytes) a 64-bit JVM will require more memory than a 32-bit JVM for the same task.

1
  • Also note that a 32-bit JVM on a 64-bit system may have more memory available than a 32-bit JVM on a 32-bit system, so it might be an interesting option if you have a "use a few GB of memory" application. Jun 7, 2013 at 10:03
3

The 32-bit vs 64-bit difference does become more important when you are interfacing with native libraries. 64-bit Java will not be able to interface with a 32-bit non-Java dll (via JNI)

1
  • 5
    You did not provide anything new to this very old question. Sep 25, 2012 at 2:33
2

Add a paramter as below in you in configuration while creating the exe

http://www.technimi.com/index.php?do=/group/java/forum/building-an-exe-using-launch4j-for-32-bit-jvm/

I hope it helps.

thanks...

/jav

0

The Java JNI requires OS libraries of the same "bittiness" as the JVM. If you attempt to build something that depends, for example, on IESHIMS.DLL (lives in %ProgramFiles%\Internet Explorer) you need to take the 32bit version when your JVM is 32bit, the 64bit version when your JVM is 64bit. Likewise for other platforms.

Apart from that, you should be all set. The generated Java bytecode s/b the same.

Note that you should use 64bit Java compiler for larger projects because it can address more memory.

-5

yo where wrong! To this theme i wrote an question to oracle. The answer was.

"If you compile your code on an 32 Bit Machine, your code should only run on an 32 Bit Processor. If you want to run your code on an 64 Bit JVM you have to compile your class Files on an 64 Bit Machine using an 64-Bit JDK."

2
  • 5
    The byte code format Java code is usually compiled to is the same regardless of 32bit or 64bit platforms. The rules are different for any native code but Java byte code is portable.
    – McDowell
    May 22, 2011 at 9:17
  • 4
    Yeah, looks like whoever at Oracle was answering your question either misunderstood it or didn't know anything about the JVM. May 22, 2011 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.