问题
I'm having this error in eclipes java.lang.UnsupportedClassVersionError: Bad version number in .class file
.
When I run javac -version
it prints
javac 1.5.0_28
Within eclipes if I right click, properties->java compiler it says I'm using 1.5
and if I then go to java build path
, I have JRE System Library [JVM 1.5.0 (MacOS X Default)]
in there.
Why am i getting this error and how do I fix it?
EDITS
java -version
prints
java version "1.5.0_28"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_28-b04-382-9M3326)
Java HotSpot(TM) Client VM (build 1.5.0_28-157, mixed mode, sharing)
The other libraries in my application is the libgdx library.
The full error is
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:676)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:317)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:375)
回答1:
You get this error when you try to run a class that was compiled for a Java version newer than what you have; for example, if you try to use a class that was compiled for Java 6 or newer on a Java 5 JVM.
It doesn't necessarily have to be your own class; you might be using some library that was built for Java 6 or newer.
Are you using specific libraries (JAR files)? Check if these are compatible with Java 5 or not. Or upgrade your Java version to Java 6.
回答2:
My guess is that you are using 3rd party libraries built with Java 1.6.
By the way is there any reason you are using Java 1.5? Java 1.6 is out for long and Java 1.5 will no longer be supported real soon (if not already).
回答3:
You have a class compiled for a newer Java than the one you are using.
The stack trace should show the name of the troublesome class. Perhaps it is a library you are using?
回答4:
You may find that the version of java that is being used is not the copy that the code was expecting.
For example, if your $PATH (this example is on Unix) is set to /usr/bin:(other directories) you would get /usr/bin/java when you run java:
$ java -version java version "1.5.0_18" Java(TM) Platform, Standard Edition for Business (build 1.5.0_18-b02) Java HotSpot(TM) Server VM (build 1.5.0_18-b02, mixed mode) $ which java /usr/bin/java
...but your program needs something like this to work:
$ /usr/local/java/jdk1.6/bin/java -version java version "1.6.0_14" Java(TM) SE Runtime Environment (build 1.6.0_14-b08) Java HotSpot(TM) Server VM (build 14.0-b16, mixed mode)
so the fix would be to add the directory where your correct version of java lives to the start of your path before calling java, or call the good version java by its absolute path.
回答5:
If upgrading your JRE is not an option, you can compile your code with lower java version.
From eclipise:
Project properties > Java compiler > Compiler compliance level> [Set to whatever JRE version you have]
See below screen for setting JRE 1.5 compatibility
来源:https://stackoverflow.com/questions/6493355/java-lang-unsupportedclassversionerror-bad-version-number-in-class-file