stad02
Posts: 2
Joined: Wed May 21, 2014 8:03 am

Java Problem - major.minor version 51.0

Wed May 21, 2014 11:13 am

Hi,

I am relatively new to using the Raspberry Pi and Java so I apologise if the answer to my issue appears obvious to you all. I installed Java using:
sudo apt-get update && sudo apt-get install oracle-java7-jdk

When I check the version installed via terminal I get the following outputs:
pi@raspberrypi ~ $ java -version
java version "1.6.0_27"
OpenJDK Runtime Environment (IcedTea6 1.12.6) (6b27-1.12.6-1~deb7u1+rpi1) OpenJDK Zero VM (build 20.0-b12, mixed mode)

pi@raspberrypi ~ $ javac -version
javac 1.7.0_40

I wrote a simple java file called HelloWorld and compiled the file. However when I try to execute the script I get the following error message:

pi@raspberrypi ~ $ java HelloWorld
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.


I've Googled the error and found various suggestions to resolve the issue; however some of these solutions are for different operating systems, devices and versions of Java. I don’t want to damage anything and feel like I’m guessing.

I’d appreciate it if someone could offer a solution or direct me to a post I’ve missed that can walk me through what I need to do.

My operating system is Rasbian Weezy.

Thanks in advance

stad02
Posts: 2
Joined: Wed May 21, 2014 8:03 am

Re: Java Problem - major.minor version 51.0

Wed May 21, 2014 11:34 am

Sorry, my post took a few hours to be approved and in the mean time I think I found a solution.

I used the instructions from point 7 of the post below:

http://stackoverflow.com/questions/1058 ... -on-ubuntu

I think that the issue that I had Java version 1.6.0 and Javac version 1.7.0

If I'm wrong and just got lucky I'd still appreciate an explanation from those in then know.

raspberrypivictim
Posts: 30
Joined: Sun Jan 22, 2012 10:35 pm

Re: Java Problem - major.minor version 51.0

Wed May 21, 2014 1:10 pm

That Java error mean you are trying to execute class files compiled with a X version of java, in a Y java virtual machine. When X version (compile) > Y version (vm)

When you execute java -version, the virtual machine, you get the 1.6
When you execute javac -version, the compiler, you get the 1.7

You are trying to execute 1.7 compiled class in a 1.6 vm. As you have said, that is the problem.


The solution is quite simple, you need to use the same java version in executing and compiling. I think you may have something wrong with the path in the raspberry system. I do not use java in the raspberry, so with this concrete subject I am afraid I cannot help you.

But in the other hand, when you compile the class files, you can use the javac -target 6 option. With this you can generate class files to run with a lower vm version.

Hope this will be helpful

Return to “Java”