Archive for November, 2009

Java Decompiler:Getting the source code from java .class file

When we compile  .java file using <code> javac</code>, we get .class file. But we can get the source code from class file, but not the full source code. <code> javap</code>, is the utility which gives details of variables and methods declared in the code.

<code>

C:\Documents and Settings\chirag.jain\Desktop>javac Test.java

C:\Documents and Settings\chirag.jain\Desktop>javap Test
Compiled from “Test.java”
public class Test extends java.lang.Object{
int number;
public Test();
public void sayHello();
}

</code>

 

To get the byte code details, use  <code> javap -c </code>

<code>

C:\Documents and Settings\chirag.jain\Desktop>javap -c Test
Compiled from “Test.java”
public class Test extends java.lang.Object{
int number;

public Test();
Code:
0:   aload_0
1:   invokespecial   #1; //Method java/lang/Object.”<init>”:()V
4:   aload_0
5:   iconst_5
6:   putfield        #2; //Field number:I
9:   return

public void sayHello();
Code:
0:   getstatic       #3; //Field java/lang/System.out:Ljava/io/PrintStream;
3:   ldc     #4; //String Hello
5:   invokevirtual   #5; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
8:   return

}

</code>

 

, , ,

Leave a comment

How to set PATH and CLASSPATH in Java

When we write a java program and put the source file(.java file) in a location other than
bin of jdk, java compiler can not find the necessary class files of java api(as java.lang.*,
for example) to compile our file.So we need to set PATH and CLASSPATH environment variables
in our system. Here are the steps:

Adding Path:
1. Go to my computer
2.Right click, go to properties.
3. Go to Advanced tab.
4. Go to Environment Variables tab.
5. Search for PATH, then click on edit.
6. Add the path of your JDK bin folder, as C:\Program Files\Java\jdk1.5.0_15\bin
7. Click OK.

Adding Classpath:

1. Go to my computer
2.Right click, go to properties.
3. Go to Advanced tab.
4. Go to Environment Variables tab.
5. Search for CLASSPATH, then click on edit.
6. If it is not there, then add it by clicking NEW.
7. Add the path of your JDK lib folder, as C:\Program Files\Java\jdk1.5.0_15\lib
7. Click OK.

By these steps,we can execute javac command under any directory in our system.
After these steps, open a new cmd prompt window, and compile your program, as:
C:\Documents and Settings\chirag.jain\Desktop>javac Program.java

After compilation, run the program as:

C:\Documents and Settings\chirag.jain\Desktop>java Program

Leave a comment