aspose file tools
The moose likes Beginning Java and the fly likes How to find the version of given class file? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to find the version of given class file?" Watch "How to find the version of given class file?" New topic
Author

How to find the version of given class file?

dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi Friends,

I am having a classfile and i would like to findout the version of javac that compiled this class. How can i do that.

Thanks in advance,

regards,
dinesh Venkatesan.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35443
    
    9
You can't. You can only determine for which version of the JVM the code was compiled. Generally, javac will generate bytecode for the JVM version that it shipped with (i.e., javac in Java 5 will create Java 5 bytecode), but that can be changed using the -source and -target switches.

Bytes 5 through 8 of a class file contain the minor and major class file version (i.e., the one specified by the "-target" switch). Some more detail can be found here.


Android appsImageJ pluginsJava web charts
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12953
    
    3

Here's a small program that shows the class file version number. However, the class file version number is not the same as the JDK version number with which it is compiled.

For Java 1.4, you'll get 48.0, for Java 5 you'll get 49.0 and for Java 6 you'll get 50.0. But as Ulf already said, you can also create version 48.0 and 49.0 files with the compiler in JDK 6, if you use the -source and -target switches.

[ August 16, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi Ulf,

Thanks much for the help. But could you tell me how to get the information that represents the major version in the file. Should I go for BCEL?

regards,
dinesh V
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35443
    
    9
What information are you looking for that Jesper's code doesn't provide?

BCEL can give you this information, but that would seem to be rather more effort than the above solution.
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Yes Ulf, Jasper's code gave me the info i wanted.
Thanks Jasper
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to find the version of given class file?
 
Similar Threads
Major Version and Minor version
which jdk was used to compile this class?
version of join() method
Re-building the class files in JDK 1.6
Read class of Major Version 48 and more