Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java Versions

Reply
Thread Tools

Java Versions

 
 
Gilbert Ostlethwaite
Guest
Posts: n/a
 
      01-22-2007
Hi

Is there anyway, that given a java .class file, I can find out what
version of Java was used to compile it?

Regards
Roger

 
Reply With Quote
 
 
 
 
Danno
Guest
Posts: n/a
 
      01-22-2007

Gilbert Ostlethwaite wrote:
> Hi
>
> Is there anyway, that given a java .class file, I can find out what
> version of Java was used to compile it?
>
> Regards
> Roger


You'd get some good info here.
http://forum.java.sun.com/thread.jsp...sageID=3791496

 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      01-22-2007
Gilbert Ostlethwaite wrote:

> Is there anyway, that given a java .class file, I can find out what
> version of Java was used to compile it?


If you really want to know which compiler, and which version of which compiler,
generated the classfile, then you will find it very very difficult -- there is
no specific information about the compiler embedded in a standard classfile.
You might be able to "fingerprint" the compilers you are interested in and
recognise the idiomatic code they generate (within the freedom allowed by the
classfile format), for instance recent versions of Sun's "javac" don't generate
jsr instructions to implement try-catch-finally blocks.

But it's more likely that you want the version of the Java platform which the
classfile was generated /for/. That's not at all the same thing since, for
instance, javac has the -target parameter, so the javac from JDK 1.6 can
generate classfiles which are marked as being suitable for running on whichever
version of the platform you choose. Anyway, it's easy to read the version
numbers from classfiles, they indicate the target platform version as follows:

major minor Java platform version required to run
45 3 1.0
45 3 1.1 // same as 1.1
46 0 1.2
47 0 1.3
48 0 1.4
49 0 1.5
50 0 1.6

Note that the default target for javac has changed over the years. IIRC in JDK
1.3 and 1.4 javac defaulted to generating classfiles only marked as requiring
at least 1.2. Later versions of javac default to requiring the same version of
the platform as their corresponding JRE.

Quick-n-dirty code for reading version numbers follows.

-- chris

===================================
import java.io.*;

public class VersionReader
{
public static void
main(String[] args)
throws IOException
{
for (int i = 0; i < args.length; i++)
writeVersion(args[i]);
}

private static void
writeVersion(String filename)
throws IOException
{
// not worth doing properly...
DataInputStream in = new DataInputStream(new
FileInputStream(filename));

if (in.read() != 0xCA
|| in.read() != 0xFE
|| in.read() != 0xBA
|| in.read() != 0xBE)
{
System.err.println(filename + "is not a valid classfile");
}
else
{
int minor = in.readShort();
int major = in.readShort();
System.out.println(filename + ": " + major + " / " + minor);
}
in.close();
}
}
===================================


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple versions of java plug-in Netscape 7.2 bud Firefox 1 05-05-2005 03:11 AM
multiple versions of java plug-in support bud Java 0 05-04-2005 05:15 PM
How to fulfill contract across two different Java versions? Scott Simpson Java 3 03-24-2005 05:48 PM
use serialver to detect serialization problems between java 1.5 and earlier versions? robert_dodier@yahoo.com Java 0 01-26-2005 10:37 PM
Help with versions in java TV API samindla venkateswar rao Java 0 11-27-2004 08:48 AM



Advertisments