问题
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.
回答1:
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.
回答2:
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.
回答3:
Assuming you are using Eclipse, on a MAC you can:
- Launch
Eclipse.app
- Choose
Eclipse -> Preferences
- Choose
Java -> Installed JREs
- Click the
Add...
button - Choose
MacOS X VM
as the JRE type. Press Next. - In the "JRE Home:" field, type
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
- You should see the system libraries in the list titled "JRE system libraries:"
- Give the JRE a name. The recommended name is
JDK 1.7
. Click Finish. - 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.
- Now, create a new project. For this verification, from the menu, select
File -> New -> Java Project
. - In the dialog that appears, enter a new name for your project. For this verification, type Test17Project
- In the JRE section of the dialog, select
Use default JRE (currently JDK 1.7)
- Click Finish.
Hope this helps
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/7237536/exception-in-thread-main-java-lang-unsupportedclassversionerror-a-unsupporte