Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the target Java version for a compiled class?

Duplicate:

Tool to read and display Java .class versions

If I have a compiled Java class, is there a way to tell from just the class file what its target version compatibility is? Specifically, I have a number of class files, compiled to Java 6, which are running under Java 5 and giving the the "Unrecognized version" error. I want to be able to look at a class file and find what its target version compatibility is without running the JVM. Any ideas?

like image 597
Mike Pone Avatar asked Mar 30 '09 17:03

Mike Pone


People also ask

How can I tell what version of Java a compiled class is?

In Java, we can use javap -verbose className to print out the Java class file information; the output includes the major version number. Or we can use the javap -verbose JavaClassName | grep major to display only the major version of the Java class file.

What is Java major version?

Java 8 uses major version 52.


2 Answers

I've found this on the net and it works.

Every '.class' file starts off with the following:

  • Magic Number [4 bytes]
  • Version Information [4 bytes]

A hexdump of a '.class' file compiled with each of the following options reveals:

javac -target 1.1 ==> CA FE BA BE 00 03 00 2D
javac -target 1.2 ==> CA FE BA BE 00 00 00 2E
javac -target 1.3 ==> CA FE BA BE 00 00 00 2F
javac -target 1.4 ==> CA FE BA BE 00 00 00 30

Perhaps you could use this information to write your own '.class' file version checking utility, using Java, or perhaps a scripting or shell language ;) !

I hope this helps.

Anthony Borla

From: http://bytes.com/groups/java/16603-how-determine-java-bytecode-version

like image 101
Kalecser Avatar answered Oct 23 '22 22:10

Kalecser


You can use the javap utility that comes with the standard JDK.

javap -verbose MyClass

Compiled from “MyClass.java”
public class MyClass extends java.lang.Object
SourceFile: “MyClass.java”
minor version: 3
major version: 45
like image 43
bruno conde Avatar answered Oct 23 '22 20:10

bruno conde