javamacosunsupported-class-version

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


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)

Solution

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