Archive for category Java Compiler

“Code Too Large” problem in java

Today while browsing through the internet, I found a very strange thing. I am sure it will be a very new and unknown fact for most of the java programmers, even the well experienced ones. The reason is because while working on a project, problem of this type does not occur very often. Now what kind of problem am I talking about? Well, most of you must have had a slight idea by looking at the title of the post. It says something when we have written a very large code and the compiler produces the error. Here is the snapshot of what I tried myself after reading this thing on internet:

C:\Documents and Settings\chirag.jain\Desktop>javac LargeCode.java
LargeCode.java:3: code too large
void largeMethod()
1 error

So does the java compiler enforces any limitation on the size of the code? The answer is yes, and that boundary is 65536 Bytes. This limitation is not on the size of whole file, but on a single method.
Now lets us delve a bit in the details. When we compile a java source file(.java file), compiler produces the byte code in .class file. When the size of the byte code of a single method crosses 65536 bytes, the compiler is not able to compile that method, and gives “code too large” error for that particular method.
Now here is one thing to notice. The overall size of your class file can grow more than 65536 bytes, but the byte code for a single method should not be more than this. Notice that here I am getting this error for a method named largeMethod(), not for the whole file.
Now for the folks who want to try this by themselves. First thing is how would you generate such a large amount of code. Although there are some code generation tools like Groovy, but these are for large projects. To try it by yourselves, you can do what I did. Here is my code:

import java.io.*;

class WriteFile
{
public static void main(String args[])
{
BufferedWriter bw=null;
try{
File f= new File("LargeCode.java");
FileWriter fr= new FileWriter(f);
bw= new BufferedWriter(fr);
String s= "System.out.println(\"hello\");";
for(int i=0;i<10000;i++)
{
bw.write(s);
}

bw.close();
}
catch(Exception e) { }
}
}

Here I have generated a new file using java IO API. It writes the statement
System.out.println("hello");
10,000 times in a separate file. Now you can add other things (class name, method name) to compile the program. If you write the whole code in a single method and compile it, you will get the error.

,

Leave a comment

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