Hello World (using the command line)

A Java program (in its uncompiled form) is essentially just one or more text files.

Importantly, by "text", we mean unformatted text -- not the kind of text you create by default in MS Word, for instance, which can be made bold, italic, big, small, etc... As such, the first thing you need to do is fire up your favorite text editor...

There is a tradition that the first program one should attempt to write in any language being learned should be one that prints the message "Hello World!" to the screen. Seriously, this idea has been around a really long time -- since Brian Kernighan used it in an internal memorandum named "Programming in C: A Tutorial" at Bell Laboratories in 1974. Let's see what writing and running such a program would entail with Java...

There are really just 3 steps:

  1. First, you need to use a text editor of your choice to create a file that contains the following text:

    public class HelloPrinter {
         public static void main(String[] args) {
              System.out.println("Hello World!");

    Type things into your file exactly as you see it above, as java is very case-sensitive and will either complain or (worse) simply not work at all if you are not careful. Save this file as HelloPrinter.java in a directory of your choosing.

  2. Next, we must compile the program into code that the java virtual machine can read. We use the command javac (short for "java compiler") to this end. Open a terminal (mac), or powershell window (pc), and navigate to the directory where HelloPrinter.java was saved. Then type the following after the prompt "$", again being careful about the capitalizations seen:

    $ javac HelloPrinter.java

    If everything went well, then if you look inside this directory (either by opening the corresponding window, or by using ls on the command line) you should see a new file called HelloPrinter.class.

    (Note: if things didn't go well and the computer complained about not recognizing javac or some such nonsense, and if you are using Windows -- you might want to make sure your PATH variable is set properly)

  3. Finally, to run the program from the command line, type the following after the prompt:

    $ java HelloPrinter

    This tells the java virtual machine to look for the class HelloPrinter.class and once found, execute the code contained within its "main" method. You can read more about the "main" method later -- but as you might glean from the code above, this means that the system should print out "Hello World!" on the screen, as seen below:

    $ java HelloPrinter
    Hello World!