How Java Class files are different ?

JavaYesterday one of my friend asked me a question “What is the difference between java class file and c++ class file? What will happen if I provide c++ class file to JVM ?”. The question is really a good question so I thought of writing this article to explain the Myths of java class file and a small comparison with malicious or class files from C++.  These class files are completely different from byte code to class loading and linking. Okie lets get down to business. Now answering the question mentioned above, Java class files are specifically designed for JVM (Java Virtual Machine) and all the .class files will be loaded and safely executed inside JVM.

The bytecodes generated by C++ are nothing but native code and linking needs to be done to get the executable code. If you change the operating system then the source code needs to be recompiled and linked again to get the executable making the whole process painful. But Java bytecodes are mainly for the JVM rather than for the underlying platform (Operating System). JVM will do the linking of classes while loading the classes. This makes the java class file portable and no recompilation is required. This makes java platform independent and all you need is the right version of JVM.

 Cafebabe

The byte code information present inside the class file are meant to be right order and JVM verifies that whenever it loads the class inside JVM. We can divide the java .class file structure in 10 sections which Magic Number, Class Version, Constant pool,flags,class,super class, interface, fields, methods and attributes. So whenever JVM loads a class, it checks for the Magic word ‘CAFEBABE’ in the first four bytes of the .class file and rejects everything else. Hope most of us are not aware of this fact and even some java programmers. See the screenshot above from a hex editor. Hope this brief explanation helps and this can sometimes be a Java Interview question.

Share or Bookmark:
  • Sphinn
  • del.icio.us
  • Mixx
  • Google
  • BlinkList
  • Furl
  • StumbleUpon
  • TwitThis
  • YahooMyWeb

Related posts:

  1. Decompile java code with Cavaj
  2. How to redirect console output to file using Java?
  3. How to remove java older versions?
  4. Java decompilers
  5. Invoking an EJB deployed in WAS using Java Client

1 Comment »

  1. Hi..it was a gripping post hi……

    Comment by seO bedrijf — June 2, 2009 @ 3:36 am

RSS feed for comments on this post. TrackBack URL

Leave a comment