Thursday, July 24, 2014

How to run a Java Program ?

 There are 3 steps to run a java program .


STEP 1 : Save the java program by the any name and with .java extension. 
            For example A.java. 

STEP 2 : Now next step is to compile the source code file i.e. the .java file.
             Open the command prompt and set the path where the java program              has been saved (by cd command). 
             To compile the source file, type as
             javac filename 
             
             For example , 
             javac A.java 
             
             After pressing enter a class file get generated (with .class                            extension) , like in above example source file A.java get compiled                  into a new class file A.class .

STEP 3 : To Run a java program we type a command as,
             java name of the class containing the main() method

             For example , 
             java myclass 

             After pressing enter, execution of its main() method starts and keep              running till all the code in main() method is finished.

In java we can save the source file by any name. The new class file generates of the class which contain the main() method. And in java we run the .class file so thats why during running the file we run it by the class name containing the main() method.  
             
             

No comments:

Post a Comment