Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Thursday 2 June 2011

Creating and Executing Java Program

     The following steps are follwed to create and execute a java program.


1 . Create the program using any text editor such as notepad or word pad etc. The general form for saving the created program in a  file is
                                                    
                                               filename.java
                       Where,  
                                     filename - Name of the class containing the main method
                                     java         - Keyword
     Ths file is called source file. If the program has more than one class, the file name must be the name of the main class.

 

Example
                                class Sample   // main class
                                { 
                                         public static void main(String args[])
                                         {
                                                 -------------------------
                                                 -------------------------
                                          }
                                  }
               Store this program as Sample.java

2 . Compile the created programs using java compiler. The general form of compile command is
                                                       
                                                      javac sourcefilename.java
                                 Where,
                                               javac                  - name of the java compiler
                                              sourcefilename - name of the already created source file.
   
     Java compiler creates a file called class file which cotains platform independent bytecodes of the program. The compiler automatically names the class file. The general form is
              
                                                      classname.class
                                   Where,
                                                classname - name of the main method class in our program
                                                class           - keyword

Example
                 Name of our source file - Sample.java
                 Compilation command   - javac Sample.java

          This create a class file as Sample.class

3 . Run the compiled program using java interpreter. The general form of run command is

                                                     java classname
                                    Where,
                                                 java           - name of the java interpreter
                                                classname - name of the main method class in our program

      Java Interpreter produces machine code from the bytecode and run the program.

Example
                 Name of our source file - Sample.java
                 Name of class file           - Sample.class

     To run the program give the run command at the command prompt as

                                                   c>java Sample

No comments:

Post a Comment

Followers