Exception in thread "main" java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0) [duplicate]
Asked Answered
D

5

90

Possible Duplicate:
unsupported major .minor version 51.0

I installed JDK7, a simple hello word program gets compile but when I run this I got following exception.

Exception in thread "main" java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0)
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

I checked java -version on command prompt, it shows Java version 1.4.2_03 but when I tried to install new java version from java.com it says that I'm having recommended Java 7 version.

Disembodied answered 30/8, 2011 at 0:31 Comment(4)
Note that you can compile for an earlier Java runtime with the "-target" option to javac.Whorled
Not a duplicate - this problem is that the JDK7 is present, just behind JDK1.4.2 on the path.Lenette
For MacOSX the following worked on my Machine. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jreLange
Oddly enough, you'll get the same sort of error if you try to use Commons CLI 1.3 or later with Java 1.4. Apache Commons CLI 1.3 and later require at least Java 1.5; you must dig out Commons CLI 1.2 to have something that will work under Java 1.4 (possibly earlier too, since the Commons CLI 1.2 release notes do not mention any minimal Java version).Cordeliacordelie
S
36

Copy the contents of the PATH settings to a notepad and check if the location for the 1.4.2 comes before that of the 7. If so, remove the path to 1.4.2 in the PATH setting and save it.

After saving and applying "Environment Variables" close and reopen the cmd line. In XP the path does no get reflected in already running programs.

Stoffel answered 30/8, 2011 at 0:51 Comment(3)
Yes I am having an entry in path befor JDK 7's path having "H:\sw\general\oracle10g\jre\1.4.2\bin". Should I remove this and should I change PATH variable under system variable section with this modified path?Disembodied
Yes. Remove the reference to "H:\sw\...\1.4.2\bin;" from the PATH under system variable section. Apply the changes and then close the existing command line and reopen it.Stoffel
:Thanks v v v much for your precious time, it's working fine now.Disembodied
S
73

Try sudo update-alternatives --config java from the command line to set the version of the JRE you want to use. This should fix it.

Stockist answered 9/12, 2011 at 16:23 Comment(4)
Did the trick for me on Ubuntu Server 12.04 too! :)Mccown
This worked for me, but only after I restarted Eclipse.Insomnia
Thanks,its working nowExpendable
Mac: command not found update-alternativesLickerish
S
36

Copy the contents of the PATH settings to a notepad and check if the location for the 1.4.2 comes before that of the 7. If so, remove the path to 1.4.2 in the PATH setting and save it.

After saving and applying "Environment Variables" close and reopen the cmd line. In XP the path does no get reflected in already running programs.

Stoffel answered 30/8, 2011 at 0:51 Comment(3)
Yes I am having an entry in path befor JDK 7's path having "H:\sw\general\oracle10g\jre\1.4.2\bin". Should I remove this and should I change PATH variable under system variable section with this modified path?Disembodied
Yes. Remove the reference to "H:\sw\...\1.4.2\bin;" from the PATH under system variable section. Apply the changes and then close the existing command line and reopen it.Stoffel
:Thanks v v v much for your precious time, it's working fine now.Disembodied
W
10

Assuming you are using Eclipse, on a MAC you can:

  1. Launch Eclipse.app
  2. Choose Eclipse -> Preferences
  3. Choose Java -> Installed JREs
  4. Click the Add... button
  5. Choose MacOS X VM as the JRE type. Press Next.
  6. In the "JRE Home:" field, type /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
  7. You should see the system libraries in the list titled "JRE system libraries:"
  8. Give the JRE a name. The recommended name is JDK 1.7. Click Finish.
  9. Check the checkbox next to the JRE entry you just created. This will cause Eclipse to use it as the default JRE for all new Java projects. Click OK.
  10. Now, create a new project. For this verification, from the menu, select File -> New -> Java Project.
  11. In the dialog that appears, enter a new name for your project. For this verification, type Test17Project
  12. In the JRE section of the dialog, select Use default JRE (currently JDK 1.7)
  13. Click Finish.

Hope this helps

Wapentake answered 21/7, 2012 at 22:49 Comment(2)
On Mac OS X Yosemite (10.10.5) , I found the jdk at /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/Benjaminbenji
Also remember to change the build path for existing projects to use the new JDKDowzall
C
9

I had this problem, after installing jdk7 next to Java 6. The binaries were correctly updated using update-alternatives --config java to jdk7, but the $JAVA_HOME environment variable still pointed to the old directory of Java 6.

Cutie answered 24/10, 2012 at 10:33 Comment(0)
O
2

Sounds like you need to change the path to your java executable to match the newest version. Basically, installing the latest Java does not necessarily mean your machine is configured to use the latest version. You didn't mention any platform details, so that's all I can say.

Operator answered 30/8, 2011 at 0:32 Comment(9)
Thanks for your precious time. How could I change the path to my Java executeable? I am using windows xp.Disembodied
See this: java.com/en/download/help/path.xml You will probably be able to verify there where the current path is pointing and change it to be correct. Be very careful when modifying the PATH variable. You can seriously mess up your computerOperator
I had set path and JAVA_HOME to point JDK7.Disembodied
@Disembodied - do you have a CLASSPATH environment variable set? If so, it may be pointing to the wrong place. (It's better to not use CLASSPATH unless you absolutely need it.)Steele
@Adnan: the PATH must cover Java7's JRE and not the old one and you must close any opened command prompt windows and spawn a new one before changes in general environment variables configuration take effect. @Ted: nonsense. That has totally no influence on java -version.Singhalese
@BalusC: Yes the "Path" is set to new JDK but problem is still there.Disembodied
Do echo %PATH% in command prompt. Is it there? If it is, then one of the folders in the beginning of the PATH has a copy of the old java.exe. Perhaps Windows/System32 or something?Singhalese
Can anyone tell me that how should I change PATH variable under system variable section without damaging my pc. I am unable to understand from hvgotcodes provided link.Disembodied
@BalusC:echo %PATH% shows a large path and it have entry pointing to new JDK.Disembodied

© 2022 - 2024 — McMap. All rights reserved.