Java Certification, Programming, JavaBean and Object Oriented Reference Books
Compile and Run a Java Program

This tutorial will take you step by step through the process of writing a java program, compiling and running it. The best way to learn is to compile and run these programs yourself. Comments such as /* this is a comment */ or // this is another comment are inserted to explain what does the line of code do. The programs are kept simple for the purpose of concentrating on the main idea in question.

Example 1
Type the following program and save it in a file with the name "First.java"

/*
This is my first java program
*/

// This is the class definition, First is the name of the class

class First{

// this program will begin with a call to main() defined as follows

    public static void main(String args[]){
       System.out.println("My first java program");
    }
}
Compile the First program as follows
c:\jdk\application>javac First.java
I am assuming that First.java is in a directory called application
and application is in the jdk directory.
The java compiler creates a file called First.class in the directory
application (check that it is there !). This class contains
the program but in bytecode form ready to run. Run the program as follows
c:\jdk\application>java First
The output of this program is
My first java program
NOTE
1-java is case sensitive, first is different from First.
2-By convention the name of the file should match the name of the
class.
 

Return to : Java Programming Hints and Tips