5

I have a situation where the deployment platform is Java 5 and the development happens with Eclipse under Java 6 where we have established a procedure of having a new workspace created when beginning work on a given project. One of the required steps is therefore setting the compiler level to Java 5, which is frequently forgotten.

We have a test machine running the deployment platform where we can run the code we build and do initial testing on our PC's, but if we forget to switch the compiler level the program cannot run. We have a build server for creating what goes to the customer, which works well, but this is for development where the build server is not needed and would add unnecessary waits.

The question is: CAN I programmatically determine the byte code version of the current class, so my code can print out a warning already while testing on my local PC?


EDIT: Please note the requirement was for the current class. Is this available through the classloadeer? Or must I locate the class file for the current class, and then investigate that?

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Note that we have since migrated to maven, and the m2e plugin for Eclipse sets this according to the compilation settings in the pom. It is still an interesting academic problem, though. – Thorbjørn Ravn Andersen Mar 01 '13 at 07:54

5 Answers5

16

Easy way to find this to run javap on class

For more details goto http://download.oracle.com/javase/1,5.0/docs/tooldocs/windows/javap.html

Example:

M:\Projects\Project-1\ant\javap -classpath M:\Projects\Project-1\build\WEB-INF\classes -verbose com.company.action.BaseAction

and look for following lines

minor version: 0
major version: 50
Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
mm101
  • 161
  • 1
  • 2
7

You could load the class file as a resource and parse the first eight bytes.

//TODO: error handling, stream closing, etc.
InputStream in = getClass().getClassLoader().getResourceAsStream(
    getClass().getName().replace('.', '/') + ".class");
DataInputStream data = new DataInputStream(in);
int magic = data.readInt();
if (magic != 0xCAFEBABE) {
  throw new IOException("Invalid Java class");
}
int minor = 0xFFFF & data.readShort();
int major = 0xFFFF & data.readShort();
System.out.println(major + "." + minor);
McDowell
  • 107,573
  • 31
  • 204
  • 267
3

here is the Java Class File Format descriptor: Java Class File Format

and here the major version values:

public static final int JDK14_MAJOR_VERSION = 48;

public static final int JDK15_MAJOR_VERSION = 49;

public static final int JDK16_MAJOR_VERSION = 50;

Now, read the class file with Java code and check the major version to know which JVM generated it

Community
  • 1
  • 1
dweeves
  • 5,525
  • 22
  • 28
3

Take a look at question: Java API to find out the JDK version a class file is compiled for?

Community
  • 1
  • 1
Shimi Bandiel
  • 5,773
  • 3
  • 40
  • 49
2

Bytes 5 through 8 of a class file content is the version number in hex. You can use Java code (or any other language) to parse the version number.

stepanian
  • 11,373
  • 8
  • 43
  • 63