11 Replies Latest reply: Mar 16, 2009 11:49 AM by DarrylBurke RSS

    How to find JDK version from Class Class File

    807588
      Hi,

      I'm new to JAVA , Please let me know is there is any tool or procedure to find the java class version from existing class file.Please do the needful.

      Thanks in Advance
      Soundar
        • 1. Re: How to find JDK version from Class Class File
          801523
          try with
          System.out.println(System.getProperty("java.vm.version"));
          • 2. Re: How to find JDK version from Class Class File
            800308
            The first two bytes of the class file encode the version number of the "target version" of that class.

            Google around and you should find an explanation of how to interpret those first two bytes. If I recall correctly 49 was 1.5.

            I've read something on this, just can't recall where. There's every chance it was on this forum, so try the search facility (the search "box" to the right of screen).

            Cheers. Keith.
            • 3. Re: How to find JDK version from Class Class File
              800308
              pvmk wrote:
              try with
              System.out.println(System.getProperty("java.vm.version"));
              Dude, Ta... I didn't know that one...

              But it's not what OP asked... java.vm.version is the version of the running JVM, not the version of a compiled class.

              [The API|http://java.sun.com/javase/6/docs/api/java/lang/System.html#getProperties()] says:
              java.vm.version       Java Virtual Machine implementation version
              Cheers. Keith.
              • 4. Re: How to find JDK version from Class Class File
                800308
                corlettk wrote:
                The first two bytes of the class file encode the version number of the "target version" of that class.

                Google around and you should find an explanation of how to interpret those first two bytes. If I recall correctly 49 was 1.5.

                I've read something on this, just can't recall where. There's every chance it was on this forum, so try the search facility (the search "box" to the right of screen).

                Cheers. Keith.
                Found it: http://www.onicos.com/staff/iz/formats/class.html

                Edit: and it's NOT the first two bytes as I stated previously. Sorry for any confusion.

                Edited by: corlettk on 16/03/2009 19:27
                • 5. Re: How to find JDK version from Class Class File
                  DarrylBurke
                  corlettk wrote:
                  Edit: and it's NOT the first two bytes as I stated previously. Sorry for any confusion.
                  The CAFE BABE is disappointed with you.
                  • 6. Re: How to find JDK version from Class Class File
                    800308
                    So CAFE BABE is the first two bytes, is it? But wait, that's more than two bytes... I go google now. Ta.

                    ------

                    Found it... Cool!

                    http://www.ibm.com/developerworks/java/library/j-dyn0429/

                    Listing 1. Source and (partial) binary for Hello.java
                    public class Hello
                    {
                        public static void main(String[] args) {
                            System.out.println("Hello, World!");
                        }
                    }
                    
                    0000: cafe babe 0000 002e 001a 0a00 0600 0c09  ................
                    0010: 000d 000e 0800 0f0a 0010 0011 0700 1207  ................
                    0020: 0013 0100 063c 696e 6974 3e01 0003 2829  .....<init>...()
                    0030: 5601 0004 436f 6465 0100 046d 6169 6e01  V...Code...main.
                    0040: 0016 285b 4c6a 6176 612f 6c61 6e67 2f53  ..([Ljava/lang/S
                    0050: 7472 696e 673b 2956 0c00 0700 0807 0014  tring;)V........
                    0060: 0c00 1500 1601 000d 4865 6c6c 6f2c 2057  ........Hello, W
                    0070: 6f72 6c64 2107 0017 0c00 1800 1901 0005  orld!...........
                    0080: 4865 6c6c 6f01 0010 6a61 7661 2f6c 616e  Hello...java/lan
                    0090: 672f 4f62 6a65 6374 0100 106a 6176 612f  g/Object...java/
                    00a0: 6c61 6e67 2f53 7973 7465 6d01 0003 6f75  lang/System...ou
                    If I ever get James Gosling in room I'm gonna tickle him until he fesses up where and how he dreamed that one up!

                    Cheers. Keith.
                    • 7. Re: How to find JDK version from Class Class File
                      800308
                      *@OP:* Found this: http://www.rgagnon.com/javadetails/java-0544.html

                      Haven't tried it myself yet... Race ya'.
                      • 8. Re: How to find JDK version from Class Class File
                        r035198x
                        Could have just gone for the fourth chapter of the JVM specification.
                        • 9. Re: How to find JDK version from Class Class File
                          800308
                          Yeah but who read as far as chapter 4? I mean the VM spec is, IMHO, the most impeneterable tome ever assembled... and remember I've read law and data-comms. Shakespeare would weep!
                          package krc.tools;
                          
                          import java.io.DataInputStream;
                          import java.io.FileInputStream;
                          import java.io.FileNotFoundException;
                          import java.io.IOException;
                          
                          public class CheckVersionOfJavaClass
                          {
                            public static void main(String[] args) throws IOException {
                              for ( String arg : args ) {
                                checkVersion(arg);
                              }
                            }
                          
                            private static void checkVersion(String filename) throws IOException {
                              DataInputStream in = null;
                              try {
                                in = new DataInputStream(new FileInputStream(filename));
                                int magic = in.readInt();
                                if ( magic != 0xCAFEBABE ) {
                                  System.err.println(filename+" is not a valid class file!");;
                                } else {
                                  int minor = in.readUnsignedShort();
                                  int major = in.readUnsignedShort();
                                  System.out.println(filename+" "+major+"."+minor);
                                }
                              } catch(FileNotFoundException e) {
                                System.err.println(e);
                              } finally {
                                if(in!=null)in.close();
                              }
                            }
                          
                          }
                          
                          /*
                          C:\Java\home\src\krc\tools>"C:\Program Files\Java\jdk1.6.0_12\bin\java.exe" -Xms512m -Xmx1024m -enableassertions -cp C:\Java\home\cl
                          asses;C:\Java\lib\java-getopt-1.0.13.jar krc.tools.CheckVersionOfJavaClass  c:\java\home\classes\krc\tools\CheckVersionOfJavaClass.c
                          lass
                          c:\java\home\classes\krc\tools\CheckVersionOfJavaClass.class 50.0
                          Press any key to continue . . .
                          */
                          Cheers. Keith.
                          • 10. Re: How to find JDK version from Class Class File
                            807588
                            Hi,

                            Thank you very much for your kind help. Also Please let me know is there is any tool to find the version

                            Thanks in Advance
                            Soundar
                            • 11. Re: How to find JDK version from Class Class File
                              DarrylBurke
                              souajan wrote:
                              Also Please let me know is there is any tool to find the version
                              Read, copy and run the code in #9.

                              And I might as well call you a putz before Keith gets round to it!

                              db