eebbesen
eebbesen

Reputation: 5148

Are UnsupportedClassVersionError messages "Bad java version" and "Unsupported major.minor version" the same?

Older posts I see associated with UnsupportedClassVersionError have the message

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file

But when I compile a class with JDK 1.8 and run it with JDK 1.7 I get

Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 52.0
  1. Are these messages caused by the same thing but just have changed over time?
  2. Why can I find neither message in the source code for rt.jar?

Update

Here's my simple class:

public class HelloWorld{
  public static void main (String[] args) {
    System.out.println("Hello, World!");
  }
}

Compiling and running with the same version:

$ java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)
$ javac HelloWorld.java
$ file HelloWorld.class
HelloWorld.class: compiled Java class data, version 52.0
$ java HelloWorld
Hello, World!

And then running with a lower version than that which I used to compile the class:

$ $JAVA7_HOME/bin/java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
$ $JAVA7_HOME/bin/java HelloWorld
Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 52.0
  at java.lang.ClassLoader.defineClass1(Native Method)
  at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
  at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
  at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
  at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

All of this was expected, I was just wondering if the messages were equivalent because I have someone reporting 'Bad version number' and then just got curious as to why I couldn't see the message in the source code. @eugenioy's answer is what I was looking for.

Upvotes: 1

Views: 294

Answers (1)

eugenioy
eugenioy

Reputation: 12393

1) Messages are equivalent and probably either changed over time or are thrown in different parts of the code with different values.

You can see here that they should be treated equivalently: https://blogs.oracle.com/sundararajan/entry/java_class_ic_errors

2) I could not find the messages in the sources for the JDK (rt.jar). That probably means that the exception is thrown in the HotSpot JVM, and not by a class present in the JDK.

Actually, if you look at this interface in the JDK: java.lang.instrument.Instrumentation, you'll see that there are a couple of methods that throw UnsupportedClassVersionError.

But I see no implementations in the source for the JDK.

To find the place where the exceptions are thrown you probably need to look at the source code for the HotSpot JVM: https://blogs.oracle.com/sundararajan/entry/so_you_want_to_read

Upvotes: 1

Related Questions