google Ads

Sunday, July 19, 2009

How to compile and run a code

How to compile and run a code

Once the file myprogClass.java has been saved, we have to compile it and convert it into byte code.


The Java compilation does not create direct executable format. (.exe in MsDOS or MS Windows). Instead byte code for the source is generated which is run by Java Virtual Machine or JVM.


To compile a .java file we have to type

Syntax:


Prompt > javac

Example:

C:\javac myprogClass.java


To generates a file called myprogClass.class


To run a byte code (for a class), it must contain a static main() method as described above. In our case we have to type

C:\ java myprogClass


This runs the main method in that class. If no main method is found an error message saying ‘No main method found’ is displayed.

Note: Its better if you set the path to the directory in which the jdk is installed. Doing so will allow you to execute the above commands from any directory.


To do this add following line to your autoexec.bat file which is in the C:


Set path=”C:\jdk1.5\bin”


Here c:\jdk1.5\bin is the directory where java and javac are stored


Before we go ahead with programming in java it is good to know the directory structure of the JDK. It may not make much sense when looking for the first time, but it is very important from viewpoint of platform independence implementation in java.

No comments:

Post a Comment