Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Chapter 4
The Java Virtual Machine

Java source code files are compiled into .class byte code files. The .class file will often be available for a class but the corresponding .java source code file will not be. In these cases, with a little effort, it’s possible to derive an astounding amount of information from the .class file alone.

Reading Compiled Files

Does the program in Listing 4-1 look familiar? I guarantee you’ve seen it before, probably many times.

Listing 4-1 Mystery code, Version 1

   CAFEBABE0003002D002008001D07001E07000E07001C0700160A0003000
   9090004000A0A0005000B0C000C00150C0014001B0C001A001F01000770
   72696E746C6E01000D436F6E7374616E7456616C75650100136A6176612F
   696F2F5072696E7453747265616D01000A457863657074696F6E7301000F
   4C696E654E756D6265725461626C6501000A536F7572636546696C65010
   00E4C6F63616C5661726961626C6573010004436F64650100036F7574010
   015284C6A6176612F6C616E672F537472696E673B29560100106A6176612
   F6C616E672F4F626A6563740100046D61696E01000F48656C6C6F576F726
   C642E6A617661010016285B4C6A6176612F6C616E672F537472696E673B2
   9560100063C696E69743E0100154C6A6176612F696F2F5072696E7453747265616D3B010
   0106A6176612F6C616E672F53797374656D01000C48656C6C6F
   20576F726C642101000A48656C6C6F576F726C6401000328295600000002000500000000
   000200090017001900010013000000250002000100000009B200071201B60006B1000000
   0100100000000A00
   0200000005000800030001001A001F000100130000001D00010001000000052AB70008B1
   00000001
   00100000000600010000000100010011000000020018

No? What if I write it like Listing 4-2?

Listing 4-2 Mystery code, Version 2

    ??æ___-_ __”__-____”___
   ___          ___

   ___@sr_____________println__
   ConstantValue___java/io/PrintStream__
   Exceptions___LineNumberTable__
   SourceFile__LocalVariables___Code___out___(Ljava/lang/String;)V___java/lang/Object___mai
   n___HelloWorld.java___([Ljava/lang/String;)V___<init>___Ljava/io/PrintStream;___java/lang/S
   ystem__Hello World!__
   HelloWorld___()V_____________     ___________%_______
   =____?__±_________
   ______________________”________*?__±__________________________

That’s a little better. You can guess that this has something to do with Java because the word Java and various Java keywords seem to show up. There’s also the string “Hello World” repeated a couple of times. This code isn’t very long, so just maybe this is a hello world program. Then again, maybe not. Let’s look at this same program another way in Listing 4-3.

Listing 4-3 Mystery code, Version 3

   Compiled from HelloWorld.java
   class HelloWorld extends java.lang.Object
      /* ACC_SUPER bit set */
   {
      public static void main();
      HelloWorld();

   Method void main()
      0 getstatic #7 <Field java.lang.System.out Ljava/io/PrintStream;>
      3 ldc #1 <String “Hello World”>
      5 invokevirtual #8 <Method java.io.PrintStream.print(Ljava/lang/String;)V>
      8 return

   Method HelloWorld()
      0 aload_0
      1 invokespecial #6 <Method java.lang.Object.<init>()V>
      4 return

   }

Now we’re getting somewhere. This is obviously a class called HelloWorld. It extends java.lang.Object. The class has two methods. The main() method is public static and void and takes an array of strings as arguments. The constructor HelloWorld() is public and takes no arguments.

However, what are all those funky lines like these?

      0 getstatic #7 <Field java.lang.System.out Ljava/io/PrintStream;>

and

      5 invokevirtual #8 <Method
    java.io.PrintStream.print(Ljava/lang/String;)V>

That doesn’t look like Java!

Finally, let’s look at the same program one more way in Listing 4-4.

Listing 4-4 Mystery code, Version 4

   class HelloWorld {

    public static void main (String args[]) {

     System.out.println(“Hello World!”);

    }

   }


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.