# "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 -- to easily create and work with the files used in java programming -- you will want to get your hands on a good text editor. There are many text editors that one can use -- below are listed a few favorites:

Text Editors on Windows

• Notepad.exe - If you are running windows, you already have this one (look under "Start Menu -> All Programs -> Accessories")
• Notepad++ - A free, better implementation of Notepad, designed with programmers in mind
• vim - The free console-based editor of choice for many hard-core programmers (it has a significant learning curve, however)

Text Editors on a Mac

• TextEdit (in text mode) - If you are running OS X+, you already have this one -- just make sure you are running it in text mode, and not rich-text mode.
• TextWrangler - A free text editor, designed with programmers in mind
• vim - This is almost identical to the one mentioned for Windows above. It is the free console-based editor of choice for many hard-core programmers (it has a significant learning curve, however) -- and if you are running OS X+, you already have it. Before you use it, however, search for a good vim tutorial online and set aside a couple hours...
• nano - This is another console-based text editor that comes with OS X+. It has far fewer features than vim, but it is also substantially easier to learn than vim.

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!