Compilation and Execution

As I mentioned in my post regarding Bytecode, compiling and executing a program involves following steps:


Step1: Write the source code in a notepad or any text editor
Step 2:Compile the program using following syntax: -
javac Filename.java
Step3:Execute the program with the following syntax:-
java Filename
We will realize this steps by seeing an example:
/*
This program welcomes you to the world of Java
Call this file Welcome.java
*/
class Welcome
{
//Your program starts with main
public static void main(String args[])
{
String str="Java";
System.out.println("Welcome to the world of " +str);
}
}

Type the program written above in a notepad (or simply copy it into notepad).Save the file using the Save As option in the File menu. Give it file extension .java.
Save the File in bin folder of jdk (Java Development Kit).Now we are done with Step1.
Lets move to Step2.Compile the above program using syntax: -
javac Welcome.java
where javac compiler creates a file Welcome.class which contains intermediate code i.e Bytecode. If your program is typed correctly then it will be compiled successfully; otherwise the compiler will show errors. If your compiler shows errors, compare your code with above code, check what is wrong. Try again!!

If you don’t get errors this time. Congrats! You have just passed Step2.

Third step involves interpreting the instructions in .class file using interpreter called java.

Use syntax:-java Welcome
where java is interpreter and Welcome is passed as command line argument. When you run the program you will get following output: -
Welcome to the world of Java
Output: -

Previous
Next Post »