Question

I am getting this error, in my destination computer despite:

  1. My source and destination computer, both have the same version of Java installed- 1.6 26th update
  2. All files which I have compiled and run in the similar way run perfectly, except for this one- it is a Swing GUI for my application
  3. Plus I am not using any IDE, I am using plain notepad for editing

Why the UnsupportedClassVersionError despite the three constraints, and solution for it?

Note: the file is a simple GUI front end code which works perfectly on the source, which used simple libraries from AWT and SWING

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(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)
Was it helpful?

Solution

Unfortunately, the fact is your main class was, in fact, compiled with a newer version of the Java compiler than you're running it with. That or your class file was somehow horribly corrupted in some other way. Check that you don't have multiple versions of java and javac installed and on your PATH. You might also want to try passing -target 1.6 to javac; if you're running (for example) javac 1.7 this will instruct it to produce code compatible with Java 1.6.

Keep in mind that you can have different version of the JRE and JDK installed - depending on your PATH order, your system might choose Java 1.7 for javac but Java 1.6 for java.

Additionally, if you have any third-party libraries on your classpath, you should make sure they were not compiled with a newer version of Java as well. If your other classes didn't make use of the third-party library that may have masked the problem.

OTHER TIPS

It occurs when u are trying to run the class compilled in different version of JDK to be executed on different version of JRE,

Please check your jdk and JRE version to be same or compatbile. you can check the version using java -version

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top