my dog learned polymorphism*
The moose likes Java in General and the fly likes How to find which version of java was used to compile particular java class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to find which version of java was used to compile particular java class." Watch "How to find which version of java was used to compile particular java class." New topic
Author

How to find which version of java was used to compile particular java class.

Pras Tiwari
Ranch Hand

Joined: Nov 07, 2005
Posts: 186
Hi,
I have a compiled java class file say a.java, which i have taken from somewhere else. Now I would like to know with which java (JDK) version that class file was compiled.

Is there any way to find out above thing (JDK version used for compiling)?

Thanks in advance.
--Pras


********Deserve Before You Desire********
Medha Jhunjhunwala
Greenhorn

Joined: Sep 06, 2007
Posts: 9
Not sure is this will help you. Every class file has a format. the first 4 bytes have the magic number and the next 4 represent the version. Please search for class file format for more details. Don't know of any utility that gives this info straightaway.

-Medha
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

No, that is not really possible.

See this thread for a discussion of the same question.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Pras Tiwari
Ranch Hand

Joined: Nov 07, 2005
Posts: 186
Got one utility that reads bytes from .class file and interpret it:-

try {
String filename = "C:\\pal\\WTP_Workspace\\JavaSocketBridge\\src\\Test.class";
DataInputStream in = new DataInputStream(new FileInputStream(
filename));
int magic = in.readInt();
if (magic != 0xcafebabe) {
System.out.println(filename + " is not a valid class!");
}
int minor = in.readUnsignedShort();
int major = in.readUnsignedShort();
System.out.println(filename + ": " + major + " . " + minor);
in.close();
} catch (IOException e) {
System.out.println("Exception: " + e.getMessage());
}


Here the combination of major and minor version will reveal the java version against which class has been compiled.

Possible major/minor value are :
45.3=1.0
45.3=1.1
46.0=1.2
47.0=1.3
48.0=1.4
49.0=1.5
50.0=1.6

-----Pras----
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find which version of java was used to compile particular java class.