First java Program

The most common example of a first Java program is the famous Hello World! program. Before going to your first Java program you should download and install JDK (Java Development Kit) properly. The following link will guide you to download and install JDK on your system. Click here Downloading and installing JDK software

Writing a Java hello world program

You should follow three steps to completely run your first Java program.

  1. Create a source file
  2. Compile the source file
  3. Run the program

Create a source file with .java extension

In your text editor (ex: Notepad in Windows) , create a new file and save it as "HelloWorld.java" . HelloWorld is your class name and you will need your class name to be the same name as your file. Copy the following source into your HelloWorld.java file.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }

Compile the source file into a .class file

A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer. In order to compile your java file, navigate to the folder where you saved HelloWorld.java and type in javac HelloWorld.java.
C:\> javac HelloWorld.java

If there are no errors in your code, next step is to run the program.

Run the program

After successfully compile the HelloWorld.java, you will get a HelloWorld.class file in the same directory. In order to run your program, in the same directory, enter the following command at the prompt:
C:\> java HelloWorld "Hello, World"
Just after you press enter key, you will get the message "Hello, World" in the next line. Congratulations! Your first Java program completed successfully!

Path and Classpath

Before you compile and run your first Java program, you should set Java Environment Variables such as Path and ClassPath. Path is an environment variable which is used by the location of bin files(binary executable files) example- java.exe, javac.exe etc. Classpath is an environment variable which is used by the Java compiler to find location of your .class file (which is created after compile your java source file). Before you start your first Java program, just look at...... Java Path and ClassPath .