Reputation: 1633
I wanted to test the platform independence of java.So I wrote the hello world program in java and compiled it using the compiler for windows to create the HelloWorld.class file. Then I tried to run that file in linux(ubuntu).I ended up in an error.(the JRE was already installed in linux)
Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: HelloWorld. Program will exit.
Then I compiled the same program using the linux compiler and used that .class file to check whether it runs in windows.It worked perfectly.
As my knowledge on both occasions the program should have worked perfectly because the byte code file(.class) is platform independent.What has gone wrong when I tried to run the program in linux?
Upvotes: 1
Views: 303
Reputation: 168815
Getting it to work cross-versions all comes down to using the cross-compilation options of the compiler. If done correctly, code can be compiled on a Java 7 SDK and be fit to run on a 1.1 JRE (assuming you can find one).
Upvotes: 1
Reputation: 66637
HelloWorld : Unsupported major.minor version 51.0
Says your compiler version is different from runtime java version.
My understanding of
java platform independence
You need to have JVM which is compatible (same (or) higher) of the class file compiler version on runtime machine also.
Upvotes: 5
Reputation: 103777
The version of the JRE that you have installed on your Ubuntu box is earlier than the version of the JDK that compiled your file. In particular 51.0
corresponds to Java 7 I believe, so you probably have version 6 installed on Ubuntu.
You're right that the byte code format is identical across all machines. However, this is not true across future versions, of course; as new features are introduced, previous versions of the JVM are not able to understand them. The format of the bytecode used in Java 7 is not understoof by v6 JVMs.
(Incidentally, if you performed this test the other way around - compiled version 6 bytecode in Linux and ran the class on your Java 7 Windows VM, it would have run successfully.)
Upvotes: 1
Reputation: 15099
You're getting the error because you compiled the class using a different version of Java and running it in a different version of Java in Linux
Most probably your Java in Linux is an older version compared to the one used for compilation.
To try out platform independence, run the .class file you compiled on Linux on Windows
Upvotes: 2