Introduction: How to Make Your First Java Program

For many people, how computers do what they do is an incredible mystery.  This how-to guide is an attempt to put you on the path to understanding how programs work by helping you create a program.

For this guide to be effective you need to be running a version of Windows.  This is because most of the instruction will only work on Windows  You do need to be able to download and install programs, but we'll cover what program later in these instructions.

Step 1: Make a File

1. Navigate to your My Documents folder in a file explorer.  
2. In that window right-click hover over new and click on "Text Document" this will create a file name "New Text Document" with the name highlighted, so you can rename it.  
3. Click away from the document it because we will rename it later.

Step 2: Write the Framework of Your Progam

1. Open your newly created text document.  
2. Inside your file type "class MyFirstProgram {" 
Do not forget the curly brace because this tells the computer where the meat of your program is.  
3. Press enter twice to create to new lines and then type "}" this tells the computer that everything that makes up this program has been entered. 
4. At this point your program should look like the attached picture for this step.

Understanding The Code:
At this point we can actually generate the program. The only problem is it won't do anything. In fact if you tried to run the program an error message will appear that says "Error: Main method not found in class MyFirstProgram, please define method as: " and then shows a specific line of code. We will be writing in that line of code in the next step since it is an important part of the program to understand.

We are using a specific programming language called Java, and as with most languages Java has a very specific way of organizing and expressing itself this organization is called the code's syntax and must be followed or the program cannot be generated by the compiler. What we did was set up the basic framework of this program.

Every Java program is composed of Classes and Methods. Classes are more commonly known as Objects and provide us with a way to organize our code. Classes store information and instructions, our program is going to be a single class with a single instruction, or method. An easy way to think of it is that classes are an entire keyboard where the methods are the individual keys on the keyboard. Right now our program is just a empty class (A keyboard with no keys). We know this, and more importantly the compiler knows this because the first word we typed says "class".

After reading the word class Java's syntax dictates that we need to state the programs name (a label on the key), in this instance we will name our program "MyFirstProgram". We will name it this because it is descriptive of what our program is: our first one!  An important thing to note is that most programming languages will not allow spaces the names of things. This is because the use of empty space withing code helps the compiler understand what is going on.

The compiler, reading in the name of our class, expect us to show some way of denoting what part of our file constitutes the class itself to do this we use curly braces ("{" "}") to denote where it starts ("{") and where it ends ("}"). Since our file does do this when we pass it through the compiler, the compiler will produce a program, but since our class has no methods to use (keys to press) an error will be produced.

Step 3: Setup the "main" Method

1. On the second line of your program press Tab and type "public static void main(String[] args) {". 
2. Press enter twice, then tab again and type "}". 
3. Your program should now look like the picture.

Understanding the Code:
What we just wrote is the "main" instruction, or method, of our program. The "main" method of a class is quite literally the main instruction of a program. When a class contains a "main" method it allows a program to be executed. This is because when a java program is run it the first thing a computer looks for is a method named "main" and follows out all the instructions within that method. Since there are no instructions within main right now, running the program will cause nothing to happen.

In front of the main method's name are three words. These words describe specific attributes of the method. The first word describes who can use the method, and since this is the main method it must be available for everyone to use so it says "public."

The next word is optional and describes when the method can be used. The specific details of word requires a deeper knowledge to fully understand so it the author will leave it up to the reader to investigate thing more on their own to gain this understanding.

The last word described what sort of object the method produces. In this case we do not want to produce anything so we state "void" so the computer knows not to be expecting anything at the end of the instruction.

Next to main we have a left parenthesis some code and a left parenthesis followed by the curly braces. The curly braces again describe where this part of the program starts and ends. The code inside the parentheses denotes the inputs of the method or what the method expects to receive from whomever using it. In this case it expects to receive an array (denoted by "[]") of Strings to run, or, in other words, it expects to receive a list of words. They are usable by the method under the name "args" which is shorthand for "arguments". When the main method is called by a computer it puts all the words that are after it into that list and hands them over to the program.

We don't need any arguments for this program so don't worry about it, but still keep the input argument in there because your computer will get confused if it isn't.

Step 4: Write Your Instruction

1. On the empty line in the middle of the program press tab twice and type "System.out.println("Hello, World!");"

Understanding the Code:

This instruction will cause the sentence "Hello, World!" to appear where we will be executing the program from.

This part of any program is where we're telling the computer to do something. The heart of programming lies in being able to do this.  Write this line of code and you've essentially done it. "System.out" is the first part of this instruction we need to understand.  System is a class readily available at any time anywhere. Appending ".out" to System allows gives us access to a class contained in System that gives us access to the computer's output. Calling the "println" method from out will write (or print) the text passed to the method to the available output. In our case this will be the command line.

Step 5: Save Your Program

1. In your text editor click on "File" then select "Save As..."
2. In the dialogue window that appears next there is a drop down selector labeled with "Save as type:" it should currently have "Text Documents (*.txt)" click on the selector and select "All Files". 
3. By "File name:" change the name of the file to "MyFirstProgram.java".
4. Click Save.

Step 6: Install the Java JDK

1. Go to http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html click on the agree radio button.
2. Click on the download for Windows x86 .
3. Once the download is finished open the file and follow the setup.  All the default options will work.

This step installs the Java JDK, or Java Developer's Kit, which gives us access to the Java compiler and the command that allows us to run Java programs.

Step 7: Copy the Path to the Java Tools

1. Open up your window file explorer and navigate to your computer's main directory (where you find your CDs and USB sticks).
2. Double click on "Local Disk" which typical has a "(C:)" following it. 
3. Double click "Program Files"
4. Highlight any fold and press the "j" key. 
5. This should take you to you a folder named "Java" open it. 
        If you cannot find the "Java" folder go back to "Local Disk" and open "Program Files (x86)".  If you still can't find it repeat Step 6.
6. You will be presented with two folder options open the folder starting with "jdk" in lowercase. 
7. Open the folder titles "bin".
8. Click on the file path, but not on any part of the path.
9. This should highlight path like in the picture.
10. Press "crtl" and "c" at the same time this will copy that path (the path tells the computer where that directory is).

Step 8: Open the Command Prompt

1. Navigate to your My Documents directory in you file explorer.
2. Create a new folder (right click hover over "New" and click on "Folder")
3. Name it "MyFirstProgramFolder" (as soon as the folder is made type out the name).  
4. Drag your "MyFirstProgram" file to the folder.  Do not enter the folder.
5. While holding shift right click on "MyFirstProgramFolder" and select "Open command window here...".  Like in the picture.
6. The command prompt should now be open like in the second picture.

Step 9: Compile the Program

1. Once in the command prompt type ' " '
2. Right click in the window and select "Paste".  
3. The path you copied from step 7 should appear.  If not, just repeat step 7 without closing the command prompt and try again.
4. With the path pasted type ' " '
5. Type "\javac MyFirstProgram.java".
6. The command prompt cursor should go to the next line and look similar to the picture.

What's Going On

You just compiled your program.  This means that your computer now has access to a file with instructions it can understand.

Step 10: Run Your Program

1. Without closing the command prompt press on the up arrow key.  
2. This should bring up the compile command you just executed.  
3. Press delete just the Java tools directory remains (with the quotes).  
4. Type "\java MyFirstProgram" to run your program.

The result this time should be similar to the previous step, but this time a line of text should be printed out saying "Hello, world!"  Just like the picture.

For an extra challenge change the message that is printed by editing your MyFirstProgram.java file and repeating the last two steps to run your modified program.