2

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)

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

-target 1.6 only ensures the right magic number and class format for that version. To get a better test of compatibility, use both the source & -bootclasspath options in addition.
@AndrewThompson, indeed, but the problem the OP reports is specifically about having the wrong magic number and/or class format, so it's a good first step.
as a matter of fact, I am compiling the code at the destination and getting the same error!
@Purushottam, like I said, you probably have multiple versions of Java installed, and a different one is being used for java and javac
yes I had different and non-compatible versions of JDK and JRE in my path and classpath. Problem solved, thanks A lot!
1

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

1 Comment

I have mentioned that my JRE and JDK are installed of the same version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.