Writing your first Java Program

With the JDK installed we can now create our first Java program.

Creating the Simple Java Program

Open notepad and enter in the Simple Java Program. If you have forgotten the code is listed below.

1
2
3
4
5
6
7
8
public class HelloWorld
{
   public static void main(String[] args)
   {
      //Display message Hello World! on the console
      System.out.println("Hello World!");
   }
}
../../../../_images/writing-your-first-java-program-1.png

Important

Remember to save the file as HelloWorld.java.

Compiling the Simple Java Program

To compile HelloWorld.java open command prompt as Administrator.

Navigate to the location of HelloWorld.java. An example is shown below.

../../../../_images/writing-your-first-java-program-2.png

Once navigated to the correct folder you can verify HelloWorld.java is there by using the command dir.

Once HelloWorld.java is verified to be in the folder run the following command.

javac HelloWorld.java

Running the Simple Java Program

While still in the location of the HelloWorld.java in the command prompt run the command:

java HelloWorld
../../../../_images/writing-your-first-java-program-3.png

Tip

If you would like view the Java bytecode you can run the command javap -c HelloWorld.class.