4

My project consists of some third party jar files, which was compiled in different version of java. My project is using older version of java so i am getting UnsupportedClassVersionError while executing the application. Is there any other way to get the version of java/jre number[45..51] in which the class files are compiled so that i can check the jar files before using it.

Shriram
  • 4,343
  • 8
  • 37
  • 64
  • You need the version of the class files or the version of your JRE? – RealSkeptic Oct 28 '15 at 16:08
  • version of java in which class files are compiled! – Shriram Oct 28 '15 at 16:10
  • You may use asm: http://asm.ow2.org/asm40/javadoc/user/org/objectweb/asm/ClassVisitor.html#visit(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[]) – user996142 Oct 28 '15 at 16:15
  • I hope it is third party jar. Even i want this version of class files of asm.. compiled. Anyway thanks for the info! – Shriram Oct 28 '15 at 16:17

2 Answers2

12

You can use javap (with -v for verbose mode), and specify any class from the jar file. For example, looking at a Joda Time jar file:

javap -cp joda-time-2.7.jar -v org.joda.time.LocalDate

Here the -cp argument specifies the jar file to be in the classpath, the -v specifies that we want more verbose information, and then there's the name of one class in the jar file.

The output starts with:

Classfile jar:file:/c:/Users/Jon/Test/joda-time-2.7.jar!/org/joda/time/LocalDate.class
  Last modified 12-Jan-2015; size 16535 bytes
  MD5 checksum d19ebb51bc5eabecbf225945eccd23ef
  Compiled from "LocalDate.java"
public final class org.joda.time.LocalDate extends org.joda.time.base.BaseLocal implements org.joda.time.ReadablePartial,java.io.Serializable
  minor version: 0
  major version: 49

The "minor version" and "major version" bits are the ones you're interested in.

It's possible that a single jar file contains classes compiled with different versions, of course.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    To know what version printed there corresponds to what java version, see http://stackoverflow.com/questions/9170832/list-of-java-class-file-format-major-version-numbers – Wim Deblauwe Oct 28 '15 at 16:15
2

If you only have standard Unix tools available and not the javap command then you can get a class file version in the form of a hex <minor> <major> version like this:

$ dd if=YourFile.class ibs=1 skip=4 count=4 of=/dev/stdout status=none | od -An -tx2 --endian=big

Example output. 0034 is 52 and therefore this class is java 8.

 0000 0034
Andy Brown
  • 11,766
  • 2
  • 42
  • 61