开发者

java.lang.UnsupportedClassVersionError: Bad version number in .class file

I'm having thi开发者_如何学Gos 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)


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.


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).


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?


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.


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


If you observe carefully, the project modules are not in the package icon... In IntelliJ or any IDE, it should be the same... Due how .jar or any bundle works.

Follow this simple steps:

right click on project's root folder, then select 'mark as directory' option and select root folder option ...

PS: (Optional part) Excuse me for not posting any screen shots... I just felt it's lil more straight forward.

The error is trying to say that the modules are not grouped, that's why the compiler says, not in root folder... Which is true, because your modules are scattered all over the IDE... For some reason.

And as for the wording in the error... It means that the version is not included as part of the packages to be compiled, remember standard of programming... purpose of packages ... And When adding stuff to a jar... They must be grouped before. Into a folder as the root for jvm.

Thank you once more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜