Thursday, February 21, 2013

What happens when we compile a program: "javac HelloWorld"?

When you go for compilation of a simple class, what happens? A .class file is generated for that class. Yes. But what the -verbose switch tells us is appealing. I mean by javac -verbose Obviously you do not have anything to do with the information other than to learn for yourself the happenings, and also have some idea about the application's performance, if it's big. Let's go with a demo.
I have a sample class:
public class HelloWorld {
  * print "Hello World"
 public String sayHello(){
  return "Hello World";
 public void printHello(){
To compile this, I tried adding the -verbose switch as in
javac -verbose
and voila, I have a little bit more info as follows (check out the bold pieces below)

E:\Java_Bin\test>javac -verbose
[parsing started]
[parsing completed 16ms]
[search path for source files: [., C:\Program Files\Java\jdk1.5.0_22\lib]]
[search path for class files: [C:\Program Files\Java\jdk1.5.0_22\jre\lib\rt.jar, C:\Program Files\Java\jdk1.5.0_22\jre\lib\jsse.jar, C:\Program Files\Java\jdk1.5.0_22\jre\lib\jce.jar, C:\Program Files\Java\jdk1.5.0_22\jre\lib\charsets.jar, C:\Program Files\Java\jdk1.5.0_22\jre\lib\ext\dnsns.jar, C:\Program Files\Java\jdk1.5.0_22\jre\lib\ext\localedata.jar, C:\Program Files\Java\jdk1.5.0_22\jre\lib\ext\sunjce_provider.jar, C:\Program Files\Java\jdk1.5.0_22\jre\lib\ext\sunpkcs11.jar, ., C:\Program Files\Java\jdk1.5.0_22\lib]]
[loading C:\Program Files\Java\jdk1.5.0_22\jre\lib\rt.jar(java/lang/Object.class)]
[loading C:\Program Files\Java\jdk1.5.0_22\jre\lib\rt.jar(java/lang/String.class)]
[checking HelloWorld]
[loading C:\Program Files\Java\jdk1.5.0_22\jre\lib\rt.jar(java/lang/System.class)]
[loading C:\Program Files\Java\jdk1.5.0_22\jre\lib\rt.jar(java/io/PrintStream.class)]
[loading C:\Program Files\Java\jdk1.5.0_22\jre\lib\rt.jar(java/io/FilterOutputStream.class)]
[loading C:\Program Files\Java\jdk1.5.0_22\jre\lib\rt.jar(java/io/OutputStream.class)]
[wrote HelloWorld.class]
[total 156ms]

So, as I and you can see, the javac.exe did a lot of work.
♦ It went for parsing our source file to find out what all things had to be done like finding out classes and jars etc,. I guess it will also remove extra spaces or null statements like ; if found.
♦ It looked up for the classes that were being used- Object (parent by default), System and Stream classes, as we used in printing out "Hello World".
♦ Now all verifications being done, it has chosen to write down the class file from our .java source.
♦ We also found out the exact time the total process took.

Actually, there's a lot more than this in the background, like the work done by class loaders, bytecode verifiers, and many other things. Object and System and other class files that I have used in my class are located in jar files as reflected above, so they have been utilized after verification only. Lot more than this, but did you get a taste!!
As I get more enlightment, the follow-up information article will be here.

No comments:

Post a Comment

Liked or hated the post? Leave your words of wisdom! Thank you :)