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.

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.