SlideShare a Scribd company logo
1 of 5
Download to read offline
© 2015. Copyright Ganesh Samarthyam. All Rights Reserved.
Disassembler	
  for	
  Java	
  (with	
  API)	
  	
  
Many students contact me asking for project ideas. If you are a Bachelor or Master’s
student in computer science/IT and looking for a good summer project idea, this is for
you. You can complete this very interesting project in 2 months.
	
  
javap	
  is	
  a	
  Java	
  “disassembler”	
  tool	
  that	
  reads	
  the	
  Java	
  class	
  files	
  and	
  dumps	
  in	
  the	
  info	
  in	
  a	
  human	
  readable	
  form.	
  
The	
  javap	
  is	
  tool	
  is	
  available	
  as	
  part	
  of	
  the	
  JDK	
  (Java	
  Development	
  Kit)	
  in	
  the	
  bin	
  directory	
  –	
  the	
  same	
  directory	
  in	
  
which	
  the	
  javac	
  –	
  the	
  Java	
  compiler	
  is	
  available.	
  	
  
	
  
Java	
  programs	
  are	
  compiled	
  into	
  intermediate	
  code	
  known	
  as	
  byte	
  codes	
  and	
  are	
  stored	
  as	
  ".class"	
  files.	
  These	
  
".class"	
  files	
  have	
  these	
  byte	
  codes	
  and	
  more	
  -­‐	
  this	
  "additional"	
  information	
  is	
  known	
  as	
  metadata.	
  JVM	
  uses	
  these	
  
byte	
  codes	
  and	
  metadata	
  to	
  execute	
  the	
  program.	
  	
  
	
  
Here	
  is	
  how	
  you	
  execute	
  the	
  tool	
  in	
  command-­‐line.	
  Consider	
  this	
  simple	
  program:	
  
	
  
C:>	
  type	
  Hello.java	
  	
  
class	
  Hello	
  {	
  
public	
  static	
  void	
  main(String	
  []args)	
  {	
  
System.out.println("hello	
  world");	
  
}	
  
}	
  
	
  	
  	
  
C:>	
  javap	
  -­‐c	
  Hello.class	
  
Compiled	
  from	
  "Hello.java"	
  
class	
  Hello	
  {	
  
	
  	
  Hello();	
  
	
  	
  	
  	
  Code:	
  
	
  	
  	
  	
  	
  	
  	
  0:	
  aload_0	
  	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  1:	
  invokespecial	
  #1	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Method	
  java/lang/Object."<init>":()V	
  
	
  	
  	
  	
  	
  	
  	
  4:	
  return	
  	
  	
  	
  	
  	
  	
  	
  	
  
	
  
	
  	
  public	
  static	
  void	
  main(java.lang.String[]);	
  
	
  	
  	
  	
  Code:	
  
	
  	
  	
  	
  	
  	
  	
  0:	
  getstatic	
  	
  	
  	
  	
  #2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Field	
  java/lang/System.out:Ljava/io/PrintStream;	
  
	
  	
  	
  	
  	
  	
  	
  3:	
  ldc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #3	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  String	
  hello	
  world	
  
	
  	
  	
  	
  	
  	
  	
  5:	
  invokevirtual	
  #4	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Method	
  java/io/PrintStream.println:(Ljava/lang/String;)V	
  
	
  	
  	
  	
  	
  	
  	
  8:	
  return	
  	
  	
  	
  	
  	
  	
  	
  	
  
}	
  
	
  
Here	
  is	
  a	
  quick	
  intro	
  on	
  bytecodes.	
  The	
  intermediate	
  language	
  for	
  Java	
  is	
  byte-­‐codes.	
  They	
  are	
  assembly	
  language	
  
code	
  for	
  a	
  imaginary	
  stack	
  based	
  machine	
  (that	
  is	
  the	
  reason	
  why	
  the	
  Java	
  runtime	
  is	
  called	
  as	
  a	
  ‘virtual	
  machine’).	
  
The	
  size	
  of	
  an	
  instruction	
  is	
  one	
  byte,	
  and	
  hence	
  the	
  name	
  ‘byte	
  code’.	
  	
  
	
  
Let	
  us	
  take	
  a	
  simple	
  example	
  and	
  understand	
  how	
  the	
  bytecodes	
  work.	
  Here	
  is	
  the	
  code	
  that	
  swaps	
  values	
  of	
  two	
  
integer	
  variables	
  named	
  x	
  and	
  y:	
  
	
  	
  
int	
  x	
  =	
  10,	
  y	
  =	
  20;	
  
int	
  temp	
  =	
  x;	
  
x	
  =	
  y;	
  
© 2015. Copyright Ganesh Samarthyam. All Rights Reserved.
y	
  =	
  temp;	
  
	
  
Here	
  is	
  the	
  generated	
  bytecode,	
  obtained	
  by	
  using	
  javap	
  tool:	
  
	
  
	
  	
  	
  	
  	
  	
  	
  0:	
  bipush	
  	
  	
  	
  	
  	
  	
  	
  10	
  
	
  	
  	
  	
  	
  	
  	
  2:	
  istore_1	
  
	
  	
  	
  	
  	
  	
  	
  3:	
  bipush	
  	
  	
  	
  	
  	
  	
  	
  20	
  
	
  	
  	
  	
  	
  	
  	
  5:	
  istore_2	
  
	
  	
  	
  	
  	
  	
  	
  6:	
  iload_1	
  
	
  	
  	
  	
  	
  	
  	
  7:	
  istore_3	
  
	
  	
  	
  	
  	
  	
  	
  8:	
  iload_2	
  
	
  	
  	
  	
  	
  	
  	
  9:	
  istore_1	
  
	
  	
  	
  	
  	
  	
  10:	
  iload_3	
  
	
  	
  	
  	
  	
  	
  11:	
  istore_2	
  	
  
	
  
There	
  are	
  only	
  a	
  few	
  instructions	
  that	
  we	
  need	
  to	
  understand	
  to	
  interpret	
  how	
  this	
  instruction	
  sequence	
  works.	
  
The	
  instruction	
  bipush	
  loads	
  (or	
  pushes)	
  the	
  argument	
  to	
  the	
  stack.	
  The	
  instruction	
  prefized	
  with	
  i	
  –	
  as	
  in	
  iload	
  and	
  
istore	
  –	
  indicates	
  that	
  the	
  instruction	
  is	
  meant	
  for	
  integers	
  (remember	
  that	
  x	
  and	
  y	
  are	
  integers).	
  The	
  instructions	
  
iload	
  and	
  istore	
  loads	
  and	
  stores	
  an	
  integer	
  to	
  and	
  from	
  the	
  ‘runtime	
  execution	
  stack’	
  and	
  ‘local	
  memory	
  area	
  in	
  
the	
  stack’.	
  Here	
  the	
  suffix	
  _1	
  and	
  _2	
  indicates	
  the	
  first	
  two	
  local	
  variables	
  x	
  and	
  x;	
  the	
  suffix	
  _3	
  indicates	
  the	
  
emporary	
  variable	
  temp.	
  	
  	
  
	
  
Now	
  let	
  us	
  simulate	
  the	
  execution	
  of	
  instructions.	
  In	
  top	
  is	
  the	
  execution	
  stack	
  and	
  its	
  state;	
  in	
  the	
  bottom	
  is	
  the	
  
value	
  for	
  memory	
  locations	
  x,	
  y,	
  and	
  temp	
  and	
  are	
  indicated	
  by	
  ‘_1’,	
  ‘_2’,	
  and	
  ‘_3’.	
  	
  
	
  
	
  
© 2015. Copyright Ganesh Samarthyam. All Rights Reserved.
	
  
	
  
The	
  basic	
  unit	
  of	
  input	
  to	
  it	
  is	
  a	
  .java	
  file.	
  The	
  Java	
  compiler	
  is	
  no	
  different	
  from	
  compilers	
  of	
  other	
  languages	
  that	
  it	
  
is	
  firmly	
  based	
  on	
  the	
  conventional	
  compiler	
  techniques,	
  but	
  the	
  code	
  it	
  generates	
  in	
  not	
  the	
  conventional	
  object	
  
code	
  for	
  that	
  particular	
  platform	
  but	
  platform	
  independent	
  .class	
  file	
  that	
  is	
  generated	
  for	
  each	
  and	
  every	
  class	
  or	
  
interface	
  defined.	
  	
  
	
  
Bytecodes	
  -­‐	
  the	
  instructions	
  for	
  a	
  hypothetical	
  microprocessor	
  -­‐	
  are	
  platform	
  independent	
  codes	
  that	
  form	
  the	
  
basis	
  of	
  the	
  platform	
  independence	
  by	
  Java.	
  They	
  are	
  targeted	
  at	
  the	
  stack-­‐oriented	
  approach.	
  All	
  the	
  evaluation	
  is	
  
done	
  in	
  the	
  stack	
  by	
  execution	
  of	
  the	
  byte	
  codes.	
  For	
  example,	
  given	
  the	
  integer	
  values	
  a	
  and	
  b,	
  the	
  statement:	
  
	
  
	
   b	
  =	
  a	
  +	
  10;	
  
	
  
may	
  be	
  converted	
  to	
  Java	
  bytecodes	
  as	
  follows,	
  
	
  
iload_1	
  	
   	
   //	
  stands	
  for	
  integer	
  load	
  the	
  first	
  local	
  	
  
//	
  variable	
  into	
  the	
  operand	
  stack	
  
bipush	
  10	
   //	
  bipush	
  stands	
  for	
  push	
  ‘byte	
  integer’	
  10	
  into	
  	
  
//	
  the	
  operand	
  stack.	
  
iadd	
   	
   //	
  add	
  the	
  topmost	
  two	
  arguments	
  in	
  the	
  stack	
  
istore_2	
  	
   //	
  store	
  the	
  result	
  in	
  top	
  of	
  the	
  stack	
  to	
  the	
  	
  
//	
  second	
  variable	
  b	
  	
  
	
   	
  
A	
  Java	
  compiler,	
  irrespective	
  of	
  the	
  machine	
  it	
  is	
  compiled,	
  generates	
  the	
  same	
  code.	
  All	
  the	
  information	
  that	
  you	
  
give	
  inside	
  the	
  class	
  definition	
  are	
  converted	
  to	
  some	
  form	
  and	
  get	
  stored	
  to	
  form	
  the	
  .class	
  file	
  for	
  that	
  
corresponding	
  class.	
  
	
  
This	
  Intermediate	
  Class	
  File	
  Format	
  is	
  a	
  specification	
  given	
  by	
  Sun.	
  It	
  is	
  a	
  well	
  documented	
  format.	
  This	
  file	
  format	
  
is	
  understood	
  by	
  the	
  Java	
  virtual	
  machine	
  that	
  operates	
  on	
  and	
  executes	
  it.	
  This	
  holds	
  the	
  information	
  like	
  the	
  
methods	
  declared	
  along	
  with	
  the	
  byte	
  codes,	
  the	
  class	
  variables	
  used	
  along	
  with	
  initializing	
  values	
  (if	
  any),	
  super	
  
class	
  information,	
  signatures	
  of	
  variables	
  and	
  methods,	
  etc.	
  This	
  is	
  similar	
  to	
  the	
  .EXE	
  code	
  that	
  is	
  organized	
  in	
  a	
  
particular	
  format	
  that	
  could	
  be	
  understood	
  by	
  the	
  underlying	
  operating	
  system.	
  It	
  also	
  has	
  a	
  constant	
  pool	
  -­‐	
  a	
  per	
  
class	
  runtime	
  data	
  structure,	
  which	
  is	
  similar	
  to	
  the	
  symbol	
  table	
  created	
  by	
  the	
  compiler.	
  The	
  Java	
  class	
  file	
  format	
  
is	
  very	
  compact	
  and	
  plays	
  very	
  important	
  role	
  in	
  making	
  Java	
  platform	
  independent.	
  	
  
	
  
The	
  contents	
  of	
  a	
  .class	
  file	
  are	
  written	
  in	
  a	
  specific	
  file	
  format,	
  described	
  in	
  the	
  specification	
  of	
  the	
  Java	
  Virtual	
  
Machine.	
  Don’t	
  worry,	
  we’re	
  not	
  going	
  to	
  understand	
  this	
  complex	
  file	
  format,	
  but	
  we’ll	
  just	
  check	
  its	
  “magic	
  
© 2015. Copyright Ganesh Samarthyam. All Rights Reserved.
number”.	
  Each	
  file	
  format	
  has	
  a	
  “magic	
  number”	
  used	
  for	
  quickly	
  checking	
  the	
  file	
  format.	
  For	
  example	
  “.MZ”	
  is	
  
the	
  magic	
  number	
  (or	
  more	
  properly,	
  “magic	
  string”)	
  for	
  “.exe”	
  files	
  in	
  Windows.	
  Similarly,	
  the	
  .class	
  files	
  have	
  the	
  
magic	
  number	
  “0xCAFEBABE”	
  written	
  as	
  a	
  hexa	
  decimal	
  value.	
  These	
  magic	
  numbers	
  are	
  typically	
  written	
  as	
  first	
  
few	
  bytes	
  of	
  a	
  variable	
  length	
  file	
  format.	
  Let	
  us	
  just	
  check	
  if	
  the	
  given	
  file	
  starts	
  with	
  the	
  magic	
  number	
  
“0xCAFEBABE”	
  –	
  if	
  so,	
  it	
  could	
  be	
  a	
  valid	
  .class	
  file,	
  or	
  else	
  it’s	
  certainly	
  not	
  a	
  .class	
  file.	
  	
  	
  
	
  
import	
  java.io.FileInputStream;	
  
import	
  java.io.FileNotFoundException;	
  
import	
  java.io.IOException;	
  
import	
  java.util.Arrays;	
  
	
  
//	
  check	
  if	
  the	
  passed	
  file	
  is	
  a	
  valid	
  .class	
  file	
  or	
  not.	
  	
  
//	
  note	
  that	
  this	
  is	
  an	
  elementary	
  version	
  of	
  a	
  checker	
  that	
  checks	
  if	
  the	
  given	
  file	
  	
  
//	
  is	
  a	
  valid	
  file	
  that	
  is	
  written	
  according	
  to	
  the	
  JVM	
  specification	
  
//	
  it	
  checks	
  only	
  the	
  magic	
  number	
  	
  
	
  
class	
  ClassFileMagicNumberChecker	
  {	
  
	
   public	
  static	
  void	
  main(String	
  []args)	
  {	
  
	
   	
   if(args.length	
  !=	
  1)	
  {	
  
	
   	
   	
   System.err.println("Pass	
  a	
  valid	
  file	
  name	
  as	
  argument");	
  
	
   	
   	
   System.exit(-­‐1);	
  
	
   	
   }	
  
	
   	
   String	
  fileName	
  =	
  args[0];	
  
	
   	
   //	
  create	
  a	
  magicNumber	
  byte	
  array	
  with	
  values	
  for	
  four	
  bytes	
  in	
  0xCAFEBABE	
  	
  
	
   	
   //	
  you	
  need	
  to	
  have	
  an	
  explicit	
  down	
  cast	
  to	
  byte	
  since	
  	
  
	
   	
   //	
  the	
  hex	
  values	
  like	
  0xCA	
  are	
  of	
  type	
  int	
  	
  
	
   	
   byte	
  []magicNumber	
  =	
  {(byte)	
  0xCA,	
  (byte)0xFE,	
  (byte)0xBA,	
  (byte)0xBE};	
  	
  
	
   	
   try	
  (FileInputStream	
  fis	
  =	
  new	
  FileInputStream(fileName))	
  {	
  
	
   	
   	
   //	
  magic	
  number	
  is	
  of	
  4	
  bytes	
  –	
  	
  
//	
  use	
  a	
  temporary	
  buffer	
  to	
  read	
  first	
  four	
  bytes	
  	
  
	
   	
   	
   byte[]	
  u4buffer	
  =	
  new	
  byte[4];	
  	
  
	
   	
   	
   //	
  read	
  a	
  buffer	
  full	
  (4	
  bytes	
  here)	
  of	
  data	
  from	
  the	
  file	
  	
  
	
   	
   	
   if(fis.read(u4buffer)	
  !=	
  -­‐1)	
  {	
  //	
  if	
  read	
  was	
  successful	
  	
  	
  
	
   	
   	
   	
   //	
  the	
  overloaded	
  method	
  equals	
  for	
  two	
  byte	
  arrays	
  	
  
//	
  checks	
  for	
  equality	
  of	
  contents	
  	
  	
  
	
   	
   	
   	
   if(Arrays.equals(magicNumber,	
  u4buffer))	
  {	
  
System.out.printf("The	
  magic	
  number	
  for	
  passed	
  file	
  %s	
  matches	
  that	
  
of	
  a	
  .class	
  file",	
  fileName);	
  
	
   	
   	
   	
   }	
  
	
   	
   	
   	
   else	
  {	
  
System.out.printf("The	
  magic	
  number	
  for	
  passed	
  file	
  %s	
  does	
  not	
  
match	
  that	
  of	
  a	
  .class	
  file",	
  fileName);	
  
	
   	
   	
   	
   }	
  
	
   	
   	
   }	
  
	
   	
   }	
  catch(FileNotFoundException	
  fnfe)	
  {	
  
	
   	
   	
   System.err.println("file	
  does	
  not	
  exist	
  with	
  the	
  given	
  file	
  name	
  ");	
  
	
   	
   }	
  catch(IOException	
  ioe)	
  {	
  
System.err.println("an	
  I/O	
  error	
  occurred	
  while	
  processing	
  the	
  file");	
  
	
   	
   }	
  
	
   }	
  
}	
  
	
  
Let’s	
  first	
  see	
  if	
  it	
  works	
  by	
  passing	
  the	
  source	
  (.java)	
  file	
  and	
  the	
  .class	
  file	
  for	
  the	
  same	
  program:	
  	
  	
  	
  
© 2015. Copyright Ganesh Samarthyam. All Rights Reserved.
	
  
D:>	
  java	
  ClassFileMagicNumberChecker	
  ClassFileMagicNumberChecker.java	
  
The	
  magic	
  number	
  for	
  passed	
  file	
  ClassFileMagicNumberChecker.java	
  does	
  not	
  match	
  that	
  of	
  a	
  .class	
  file	
  
D:>	
  java	
  ClassFileMagicNumberChecker	
  ClassFile	
  
MagicNumberChecker.class	
  
The	
  magic	
  number	
  for	
  passed	
  file	
  ClassFileMagicNumberChecker.class	
  matches	
  that	
  of	
  a	
  .class	
  file	
  
	
  	
  
You	
  need	
  to	
  extend	
  this	
  program	
  by	
  reading	
  the	
  file	
  format	
  and	
  print	
  the	
  content	
  in	
  a	
  readable	
  format.	
  The	
  output	
  
should	
  be	
  same	
  as	
  the	
  javap	
  output.	
  	
  
	
  
Testcases:	
  Test	
  your	
  tool	
  implementation	
  by	
  disassembling	
  the	
  class	
  files	
  that	
  are	
  provided	
  as	
  part	
  of	
  the	
  Java	
  
runtime	
  library	
  (i.e.,	
  test	
  your	
  tool	
  for	
  the	
  class	
  files	
  provided	
  within	
  “rt.jar”	
  file	
  that	
  is	
  available	
  as	
  part	
  of	
  the	
  
JDK/JRE	
  folder).	
  	
  	
  
Resources	
  for	
  the	
  project:	
  	
  
• Wikipedia	
  overview	
  page	
  for	
  the	
  Java	
  class	
  file	
  format:	
  http://en.wikipedia.org/wiki/Java_class_file	
  
• Wikipedia	
  overview	
  page	
  for	
  Java	
  bytecodes:	
  http://en.wikipedia.org/wiki/Java_bytecode	
  
• The	
  Java	
  class	
  file	
  format	
  is	
  documented	
  here:	
  http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-­‐
4.html#jvms-­‐4.4	
  
• The	
  Java	
  Virtual	
  Machine	
  specification	
  book	
  is	
  available	
  for	
  download	
  for	
  free(!)	
  here:	
  
http://docs.oracle.com/javase/specs/jvms/se7/jvms7.pdf	
  .	
  This	
  book	
  includes	
  the	
  specification	
  for	
  the	
  
class	
  file	
  format.	
  	
  
Resources	
  for	
  learning	
  Java:	
  	
  
• If	
  you	
  are	
  new	
  to	
  Java,	
  you	
  can	
  get	
  started	
  from	
  here:	
  
http://www.oracle.com/technetwork/topics/newtojava/overview/index.html
• The	
  free	
  online	
  Oracle	
  Java	
  tutorial	
  is	
  an	
  excellent	
  resource	
  to	
  learn	
  Java:	
  
http://docs.oracle.com/javase/tutorial/.	
  It	
  is	
  a	
  also	
  available	
  as	
  a	
  free	
  book	
  as	
  part	
  of	
  the	
  tutorial	
  bundle;	
  
download	
  it	
  here:	
  http://download.oracle.com/javase/tutorial/	
  
• When	
  you	
  are	
  learning	
  Java	
  and	
  writing	
  code,	
  you’ll	
  need	
  to	
  consult	
  the	
  API	
  of	
  the	
  Java	
  standard	
  library.	
  
The	
  documentation	
  is	
  available	
  here:	
  http://download.oracle.com/javase/7/docs/api/	
  
• “Head	
  first	
  Java”	
  by	
  Kathy	
  Sierra	
  and	
  Bert	
  Bates	
  is	
  a	
  fun	
  and	
  easy	
  book	
  to	
  learn	
  Java;	
  download	
  the	
  free	
  e-­‐
book	
  version	
  here:	
  http://it-­‐ebooks.info/book/255/.	
  
• “Thinking	
  in	
  Java”	
  by	
  Bruce	
  Eckel	
  is	
  a	
  very	
  good	
  book	
  to	
  learn	
  Java	
  well.	
  Download	
  it	
  here:	
  
	
  http://www.mindviewinc.com/Books/downloads.html	
  	
  	
  	
  	
  
• If	
  you	
  have	
  queries	
  or	
  doubts,	
  you	
  can	
  get	
  them	
  clarified	
  in	
  “Beginning	
  Java”	
  CodeRanch	
  forum:	
  
http://www.coderanch.com/forums/f-­‐33/java	
  

More Related Content

What's hot

What's hot (19)

Introduction to the Java(TM) Advanced Imaging API
Introduction to the Java(TM) Advanced Imaging APIIntroduction to the Java(TM) Advanced Imaging API
Introduction to the Java(TM) Advanced Imaging API
 
Java features
Java featuresJava features
Java features
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Java notes
Java notesJava notes
Java notes
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Java session08
Java session08Java session08
Java session08
 
Let's start with Java- Basic Concepts
Let's start with Java- Basic ConceptsLet's start with Java- Basic Concepts
Let's start with Java- Basic Concepts
 
02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
 
Core java
Core java Core java
Core java
 
Presentation to java
Presentation  to  javaPresentation  to  java
Presentation to java
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
Core java
Core javaCore java
Core java
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 
Core java
Core javaCore java
Core java
 

Similar to Java Disassembler API Project

Java programming basics
Java programming basicsJava programming basics
Java programming basicsHamid Ghorbani
 
Java programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, MysuruJava programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, MysuruNithin Kumar,VVCE, Mysuru
 
Top 10 Important Core Java Interview questions and answers.pdf
Top 10 Important Core Java Interview questions and answers.pdfTop 10 Important Core Java Interview questions and answers.pdf
Top 10 Important Core Java Interview questions and answers.pdfUmesh Kumar
 
Java for Mainframers
Java for MainframersJava for Mainframers
Java for MainframersRich Helton
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction AKR Education
 
Java questions with answers
Java questions with answersJava questions with answers
Java questions with answersKuntal Bhowmick
 
Bt0074 oops with java2
Bt0074 oops with java2Bt0074 oops with java2
Bt0074 oops with java2Techglyphs
 
Java-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oopsJava-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oopsbuvanabala
 
Java byte code & virtual machine
Java byte code & virtual machineJava byte code & virtual machine
Java byte code & virtual machineLaxman Puri
 
Java Virtual Machine - Internal Architecture
Java Virtual Machine - Internal ArchitectureJava Virtual Machine - Internal Architecture
Java Virtual Machine - Internal Architecturesubnesh
 

Similar to Java Disassembler API Project (20)

Java programming basics
Java programming basicsJava programming basics
Java programming basics
 
1.introduction to java
1.introduction to java1.introduction to java
1.introduction to java
 
OOP-Chap2.docx
OOP-Chap2.docxOOP-Chap2.docx
OOP-Chap2.docx
 
java notes.pdf
java notes.pdfjava notes.pdf
java notes.pdf
 
Java programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, MysuruJava programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, Mysuru
 
Introduction java programming
Introduction java programmingIntroduction java programming
Introduction java programming
 
Top 10 Important Core Java Interview questions and answers.pdf
Top 10 Important Core Java Interview questions and answers.pdfTop 10 Important Core Java Interview questions and answers.pdf
Top 10 Important Core Java Interview questions and answers.pdf
 
Java introduction
Java introductionJava introduction
Java introduction
 
JAVA Program Examples
JAVA Program ExamplesJAVA Program Examples
JAVA Program Examples
 
Java for Mainframers
Java for MainframersJava for Mainframers
Java for Mainframers
 
Basic Java I
Basic Java IBasic Java I
Basic Java I
 
JAVA for Every one
JAVA for Every oneJAVA for Every one
JAVA for Every one
 
basic_java.ppt
basic_java.pptbasic_java.ppt
basic_java.ppt
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction
 
Java questions with answers
Java questions with answersJava questions with answers
Java questions with answers
 
Bt0074 oops with java2
Bt0074 oops with java2Bt0074 oops with java2
Bt0074 oops with java2
 
Java-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oopsJava-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oops
 
Java byte code & virtual machine
Java byte code & virtual machineJava byte code & virtual machine
Java byte code & virtual machine
 
Java basic concept
Java basic conceptJava basic concept
Java basic concept
 
Java Virtual Machine - Internal Architecture
Java Virtual Machine - Internal ArchitectureJava Virtual Machine - Internal Architecture
Java Virtual Machine - Internal Architecture
 

More from Ganesh Samarthyam

Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeGanesh Samarthyam
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”Ganesh Samarthyam
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGanesh Samarthyam
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationGanesh Samarthyam
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterGanesh Samarthyam
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Ganesh Samarthyam
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckGanesh Samarthyam
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageGanesh Samarthyam
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Ganesh Samarthyam
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz QuestionsGanesh Samarthyam
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz QuestionsGanesh Samarthyam
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizGanesh Samarthyam
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 

More from Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 

Recently uploaded

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 

Recently uploaded (20)

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 

Java Disassembler API Project

  • 1. © 2015. Copyright Ganesh Samarthyam. All Rights Reserved. Disassembler  for  Java  (with  API)     Many students contact me asking for project ideas. If you are a Bachelor or Master’s student in computer science/IT and looking for a good summer project idea, this is for you. You can complete this very interesting project in 2 months.   javap  is  a  Java  “disassembler”  tool  that  reads  the  Java  class  files  and  dumps  in  the  info  in  a  human  readable  form.   The  javap  is  tool  is  available  as  part  of  the  JDK  (Java  Development  Kit)  in  the  bin  directory  –  the  same  directory  in   which  the  javac  –  the  Java  compiler  is  available.       Java  programs  are  compiled  into  intermediate  code  known  as  byte  codes  and  are  stored  as  ".class"  files.  These   ".class"  files  have  these  byte  codes  and  more  -­‐  this  "additional"  information  is  known  as  metadata.  JVM  uses  these   byte  codes  and  metadata  to  execute  the  program.       Here  is  how  you  execute  the  tool  in  command-­‐line.  Consider  this  simple  program:     C:>  type  Hello.java     class  Hello  {   public  static  void  main(String  []args)  {   System.out.println("hello  world");   }   }         C:>  javap  -­‐c  Hello.class   Compiled  from  "Hello.java"   class  Hello  {      Hello();          Code:                0:  aload_0                              1:  invokespecial  #1                                    //  Method  java/lang/Object."<init>":()V                4:  return                        public  static  void  main(java.lang.String[]);          Code:                0:  getstatic          #2                                    //  Field  java/lang/System.out:Ljava/io/PrintStream;                3:  ldc                      #3                                    //  String  hello  world                5:  invokevirtual  #4                                    //  Method  java/io/PrintStream.println:(Ljava/lang/String;)V                8:  return                   }     Here  is  a  quick  intro  on  bytecodes.  The  intermediate  language  for  Java  is  byte-­‐codes.  They  are  assembly  language   code  for  a  imaginary  stack  based  machine  (that  is  the  reason  why  the  Java  runtime  is  called  as  a  ‘virtual  machine’).   The  size  of  an  instruction  is  one  byte,  and  hence  the  name  ‘byte  code’.       Let  us  take  a  simple  example  and  understand  how  the  bytecodes  work.  Here  is  the  code  that  swaps  values  of  two   integer  variables  named  x  and  y:       int  x  =  10,  y  =  20;   int  temp  =  x;   x  =  y;  
  • 2. © 2015. Copyright Ganesh Samarthyam. All Rights Reserved. y  =  temp;     Here  is  the  generated  bytecode,  obtained  by  using  javap  tool:                  0:  bipush                10                2:  istore_1                3:  bipush                20                5:  istore_2                6:  iload_1                7:  istore_3                8:  iload_2                9:  istore_1              10:  iload_3              11:  istore_2       There  are  only  a  few  instructions  that  we  need  to  understand  to  interpret  how  this  instruction  sequence  works.   The  instruction  bipush  loads  (or  pushes)  the  argument  to  the  stack.  The  instruction  prefized  with  i  –  as  in  iload  and   istore  –  indicates  that  the  instruction  is  meant  for  integers  (remember  that  x  and  y  are  integers).  The  instructions   iload  and  istore  loads  and  stores  an  integer  to  and  from  the  ‘runtime  execution  stack’  and  ‘local  memory  area  in   the  stack’.  Here  the  suffix  _1  and  _2  indicates  the  first  two  local  variables  x  and  x;  the  suffix  _3  indicates  the   emporary  variable  temp.         Now  let  us  simulate  the  execution  of  instructions.  In  top  is  the  execution  stack  and  its  state;  in  the  bottom  is  the   value  for  memory  locations  x,  y,  and  temp  and  are  indicated  by  ‘_1’,  ‘_2’,  and  ‘_3’.        
  • 3. © 2015. Copyright Ganesh Samarthyam. All Rights Reserved.     The  basic  unit  of  input  to  it  is  a  .java  file.  The  Java  compiler  is  no  different  from  compilers  of  other  languages  that  it   is  firmly  based  on  the  conventional  compiler  techniques,  but  the  code  it  generates  in  not  the  conventional  object   code  for  that  particular  platform  but  platform  independent  .class  file  that  is  generated  for  each  and  every  class  or   interface  defined.       Bytecodes  -­‐  the  instructions  for  a  hypothetical  microprocessor  -­‐  are  platform  independent  codes  that  form  the   basis  of  the  platform  independence  by  Java.  They  are  targeted  at  the  stack-­‐oriented  approach.  All  the  evaluation  is   done  in  the  stack  by  execution  of  the  byte  codes.  For  example,  given  the  integer  values  a  and  b,  the  statement:       b  =  a  +  10;     may  be  converted  to  Java  bytecodes  as  follows,     iload_1       //  stands  for  integer  load  the  first  local     //  variable  into  the  operand  stack   bipush  10   //  bipush  stands  for  push  ‘byte  integer’  10  into     //  the  operand  stack.   iadd     //  add  the  topmost  two  arguments  in  the  stack   istore_2     //  store  the  result  in  top  of  the  stack  to  the     //  second  variable  b         A  Java  compiler,  irrespective  of  the  machine  it  is  compiled,  generates  the  same  code.  All  the  information  that  you   give  inside  the  class  definition  are  converted  to  some  form  and  get  stored  to  form  the  .class  file  for  that   corresponding  class.     This  Intermediate  Class  File  Format  is  a  specification  given  by  Sun.  It  is  a  well  documented  format.  This  file  format   is  understood  by  the  Java  virtual  machine  that  operates  on  and  executes  it.  This  holds  the  information  like  the   methods  declared  along  with  the  byte  codes,  the  class  variables  used  along  with  initializing  values  (if  any),  super   class  information,  signatures  of  variables  and  methods,  etc.  This  is  similar  to  the  .EXE  code  that  is  organized  in  a   particular  format  that  could  be  understood  by  the  underlying  operating  system.  It  also  has  a  constant  pool  -­‐  a  per   class  runtime  data  structure,  which  is  similar  to  the  symbol  table  created  by  the  compiler.  The  Java  class  file  format   is  very  compact  and  plays  very  important  role  in  making  Java  platform  independent.       The  contents  of  a  .class  file  are  written  in  a  specific  file  format,  described  in  the  specification  of  the  Java  Virtual   Machine.  Don’t  worry,  we’re  not  going  to  understand  this  complex  file  format,  but  we’ll  just  check  its  “magic  
  • 4. © 2015. Copyright Ganesh Samarthyam. All Rights Reserved. number”.  Each  file  format  has  a  “magic  number”  used  for  quickly  checking  the  file  format.  For  example  “.MZ”  is   the  magic  number  (or  more  properly,  “magic  string”)  for  “.exe”  files  in  Windows.  Similarly,  the  .class  files  have  the   magic  number  “0xCAFEBABE”  written  as  a  hexa  decimal  value.  These  magic  numbers  are  typically  written  as  first   few  bytes  of  a  variable  length  file  format.  Let  us  just  check  if  the  given  file  starts  with  the  magic  number   “0xCAFEBABE”  –  if  so,  it  could  be  a  valid  .class  file,  or  else  it’s  certainly  not  a  .class  file.         import  java.io.FileInputStream;   import  java.io.FileNotFoundException;   import  java.io.IOException;   import  java.util.Arrays;     //  check  if  the  passed  file  is  a  valid  .class  file  or  not.     //  note  that  this  is  an  elementary  version  of  a  checker  that  checks  if  the  given  file     //  is  a  valid  file  that  is  written  according  to  the  JVM  specification   //  it  checks  only  the  magic  number       class  ClassFileMagicNumberChecker  {     public  static  void  main(String  []args)  {       if(args.length  !=  1)  {         System.err.println("Pass  a  valid  file  name  as  argument");         System.exit(-­‐1);       }       String  fileName  =  args[0];       //  create  a  magicNumber  byte  array  with  values  for  four  bytes  in  0xCAFEBABE         //  you  need  to  have  an  explicit  down  cast  to  byte  since         //  the  hex  values  like  0xCA  are  of  type  int         byte  []magicNumber  =  {(byte)  0xCA,  (byte)0xFE,  (byte)0xBA,  (byte)0xBE};         try  (FileInputStream  fis  =  new  FileInputStream(fileName))  {         //  magic  number  is  of  4  bytes  –     //  use  a  temporary  buffer  to  read  first  four  bytes           byte[]  u4buffer  =  new  byte[4];           //  read  a  buffer  full  (4  bytes  here)  of  data  from  the  file           if(fis.read(u4buffer)  !=  -­‐1)  {  //  if  read  was  successful               //  the  overloaded  method  equals  for  two  byte  arrays     //  checks  for  equality  of  contents               if(Arrays.equals(magicNumber,  u4buffer))  {   System.out.printf("The  magic  number  for  passed  file  %s  matches  that   of  a  .class  file",  fileName);           }           else  {   System.out.printf("The  magic  number  for  passed  file  %s  does  not   match  that  of  a  .class  file",  fileName);           }         }       }  catch(FileNotFoundException  fnfe)  {         System.err.println("file  does  not  exist  with  the  given  file  name  ");       }  catch(IOException  ioe)  {   System.err.println("an  I/O  error  occurred  while  processing  the  file");       }     }   }     Let’s  first  see  if  it  works  by  passing  the  source  (.java)  file  and  the  .class  file  for  the  same  program:        
  • 5. © 2015. Copyright Ganesh Samarthyam. All Rights Reserved.   D:>  java  ClassFileMagicNumberChecker  ClassFileMagicNumberChecker.java   The  magic  number  for  passed  file  ClassFileMagicNumberChecker.java  does  not  match  that  of  a  .class  file   D:>  java  ClassFileMagicNumberChecker  ClassFile   MagicNumberChecker.class   The  magic  number  for  passed  file  ClassFileMagicNumberChecker.class  matches  that  of  a  .class  file       You  need  to  extend  this  program  by  reading  the  file  format  and  print  the  content  in  a  readable  format.  The  output   should  be  same  as  the  javap  output.       Testcases:  Test  your  tool  implementation  by  disassembling  the  class  files  that  are  provided  as  part  of  the  Java   runtime  library  (i.e.,  test  your  tool  for  the  class  files  provided  within  “rt.jar”  file  that  is  available  as  part  of  the   JDK/JRE  folder).       Resources  for  the  project:     • Wikipedia  overview  page  for  the  Java  class  file  format:  http://en.wikipedia.org/wiki/Java_class_file   • Wikipedia  overview  page  for  Java  bytecodes:  http://en.wikipedia.org/wiki/Java_bytecode   • The  Java  class  file  format  is  documented  here:  http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-­‐ 4.html#jvms-­‐4.4   • The  Java  Virtual  Machine  specification  book  is  available  for  download  for  free(!)  here:   http://docs.oracle.com/javase/specs/jvms/se7/jvms7.pdf  .  This  book  includes  the  specification  for  the   class  file  format.     Resources  for  learning  Java:     • If  you  are  new  to  Java,  you  can  get  started  from  here:   http://www.oracle.com/technetwork/topics/newtojava/overview/index.html • The  free  online  Oracle  Java  tutorial  is  an  excellent  resource  to  learn  Java:   http://docs.oracle.com/javase/tutorial/.  It  is  a  also  available  as  a  free  book  as  part  of  the  tutorial  bundle;   download  it  here:  http://download.oracle.com/javase/tutorial/   • When  you  are  learning  Java  and  writing  code,  you’ll  need  to  consult  the  API  of  the  Java  standard  library.   The  documentation  is  available  here:  http://download.oracle.com/javase/7/docs/api/   • “Head  first  Java”  by  Kathy  Sierra  and  Bert  Bates  is  a  fun  and  easy  book  to  learn  Java;  download  the  free  e-­‐ book  version  here:  http://it-­‐ebooks.info/book/255/.   • “Thinking  in  Java”  by  Bruce  Eckel  is  a  very  good  book  to  learn  Java  well.  Download  it  here:    http://www.mindviewinc.com/Books/downloads.html           • If  you  have  queries  or  doubts,  you  can  get  them  clarified  in  “Beginning  Java”  CodeRanch  forum:   http://www.coderanch.com/forums/f-­‐33/java