1

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?

2
  • 4
    Did you even search the web for that error? You're trying to run it on the wrong version of Java. Commented Sep 4, 2012 at 16:20
  • 1
    Check here: stackoverflow.com/questions/2466828/… Commented Sep 4, 2012 at 16:20

4 Answers 4

5
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.

Sign up to request clarification or add additional context in comments.

5 Comments

More specifically, he tried using Java7 compiled code on Java6 VM.
Of course you all are correct. Thanks a lot. Now I lost my doubts.. :)
@ThomasJungblut I have updated java in my linux,and now it works.
@Fazlan: If answer helped you, don't forget to accept it by clicking tick mark right beside answer. That way community shows interest in answering your question.
@ThomasJungblut I did it. I am new to this forum and thanx for teaching it. :)
2

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

Comments

1

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.)

1 Comment

Yes of course,I mentioned in the question that it worked when I tried the other way around. anyway I am updating my JRE for linux at the moment.Hope it will fix the problem.Thanks a lot :)
1

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).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.