Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find the version of given class file?  RSS feed

 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Ulf, Jasper's code gave me the info i wanted.
Thanks Jasper
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!