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.

Community
  • 1
  • 1
Adnan
  • 4,517
  • 15
  • 44
  • 54
  • 1
    Note that you can compile for an earlier Java runtime with the "-target" option to javac. – Thorbjørn Ravn Andersen Aug 30 '11 at 03:02
  • 2
    Not a duplicate - this problem is that the JDK7 is present, just behind JDK1.4.2 on the path. – Andrew Alcock Feb 04 '13 at 09:53
  • 1
    For MacOSX the following worked on my Machine. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre – Keith John Hutchison Dec 17 '14 at 20:04
  • 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). – Urhixidur May 29 '17 at 19:39

5 Answers5

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.

johnzachary
  • 2,435
  • 2
  • 19
  • 9
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.

arunmur
  • 650
  • 4
  • 5
  • 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? – Adnan Aug 30 '11 at 01:06
  • 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. – arunmur Aug 30 '11 at 01:08
  • 1
    :Thanks v v v much for your precious time, it's working fine now. – Adnan Aug 30 '11 at 01:13
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

James Raitsev
  • 92,517
  • 154
  • 335
  • 470
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.

liecno
  • 924
  • 1
  • 8
  • 18
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.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • Thanks for your precious time. How could I change the path to my Java executeable? I am using windows xp. – Adnan Aug 30 '11 at 00:37
  • 1
    See this: http://www.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 computer – hvgotcodes Aug 30 '11 at 00:39
  • I had set path and JAVA_HOME to point JDK7. – Adnan Aug 30 '11 at 00:40
  • @Adnan - 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.) – Ted Hopp Aug 30 '11 at 00:42
  • @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`. – BalusC Aug 30 '11 at 00:46
  • @BalusC: Yes the "Path" is set to new JDK but problem is still there. – Adnan Aug 30 '11 at 00:51
  • 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? – BalusC Aug 30 '11 at 00:52
  • 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. – Adnan Aug 30 '11 at 00:54
  • @BalusC:echo %PATH% shows a large path and it have entry pointing to new JDK. – Adnan Aug 30 '11 at 00:56