Introduction: How to Make Your First Java Program

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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.

Comments

MatthewD229 (author)2017-06-08

Ok next step

AlexM610 (author)2017-01-23

this is what i keep getting please help

MyFirstProgram.java:3: error: package system does not exist

system.out.PrintIn("Hello, World!");

^

1 error

AlexM610 (author)AlexM6102017-01-23

the ^ is pointing at the "." between system and out

rafael lol (author)AlexM6102017-02-11

this might be a visual issue because i can see it pointing at the s

BrianW296 (author)AlexM6102017-02-09

Alex,

In Java, some of the syntax is case sensitive. Instead of what you have typed, "system.out.Println("Hello, World!");" type this, "System.out.println("Hello, World!");".

You will notice that the 'S' in system is capitalized and the 'p' in println is lower case. These changes should help your code run successfully. Just an FYI... println means "print line" which means that each time you run the code it will print the words on a new line. You could also write "System.out.print" and the text would be written on the same line each time. Lastly, the little caret (^) is pointer showing you where your error is. It is pointing to the first s in "system" because it is not capitalized. Hope this helps. Have fun coding. :)

-- Brian

RuanB10 (author)2016-10-28

Hey TracyT46, it seems like the "MyFirstProgram.java" file is not present in the directory that you are in.

TracyT46 (author)2016-10-20

what am I doing wrong?

C:\Users\tthompson\Documents\MyFirstProgramFolder>"C:\Program Files (x86)\Java\jdk1.7.0_79\bin"\javac MyFirstProgram.java

javac: file not found: MyFirstProgram.java

Usage: javac <options> <source files>

use -help for a list of possible options

C:\Users\tthompson\Documents\MyFirstProgramFolder>"C:\Program Files (x86)\Java\jdk1.7.0_79\bin"\javac MyFirstProgram.java

javac: file not found: MyFirstProgram.java

Usage: javac <options> <source files>

use -help for a list of possible options

C:\Users\tthompson\Documents\MyFirstProgramFolder>

souravr10 (author)2016-10-14

this website is flop

sefedin (author)2016-09-23

i did that before, im famous programer i made game and any kind website so i preffer to use eclipse IDE:;//

sefedin (author)2016-09-23

good job

Davidka made it! (author)2016-08-21

Yay I made it!

Mase13 (author)2016-08-06

well i just figured out the backslash button on my keyboard doesnt work. lol.

DeathS2 (author)2016-08-03

Mine just opened up the file in notepad++?

WaseAzure (author)2016-07-30

is there any virus in the program ?

electric guy (author)2016-03-26

do I need to download some kinda special progrum to type in?

JacoF3 (author)electric guy2016-07-15

I personally Like To Use Notepad ++ But It All Depends On What You Prefer :)

Look at Step 6. ; )

CookiePie_ (author)2016-07-12

I keep getting i dont have permission, even though i am a administrator

ReeseR4 (author)2016-05-29

k nvm thx!

ReeseR4 (author)2016-05-29

Um How Did U Enter The Cmd In The File?

:/

DigvijayV (author)2016-04-20

please will anyone tell me?????

AndiB12 made it! (author)2016-04-13

At step 9 it says that it can't find the entered path please help me

DigvijayV (author)AndiB122016-04-20

same here . did you get any solution?

DigvijayV (author)2016-04-20

I am trying to make my first program . But I can't understand the 8 th step . and I can't go forward

baconbits1316 (author)2016-04-10

Once I get to step 6 and click on the link, I'm lost. Can someone please explain what to do? I have Windows 8.1, if that helps. When I get to the website, should I get 'Java SE Development Kit 7u79' or 'Java SE Development Kit 7u80'? ( I might be completely off and need to get a different program )

Masonk12 (author)2016-03-26

My local disk seems to be called "S3A8666D003"

You Can Rename It. Just Right Click it, then Click Rename.

WilliamS108 (author)2015-10-24

i have a 64x Verson Of Windows 10

Me too.

Masonk12 (author)WilliamS1082016-03-26

me too.

DawidK5 (author)WilliamS1082016-01-13

me too. You need to do the same things just download jdk for 64x

no problems

RodelG made it! (author)2016-04-02

I got a hard time dealing with this but you need to work hard to make a success.I kept rechecking my program and the finally...

AzimR1 (author)2016-02-23

when im at step 7.....eventhough i press the shift n right-click...there was no "

Open command window here" option...what should i do?

Masonk12 (author)AzimR12016-03-26

well what version of windows OR mac do you have?

Unrightful Era (author)2016-03-10

\Documents\MyFirstProgramFolder>"C:\Program Files (x86)\Java\jdk1.7.0_79\bin"\Javac MyFirstProgram.Java

error: Class names, 'MyFirstProgram.Java', are only accepted if annotation processing is explicitly requested

1 error

Kept getting this same error no matter how many times i retried it? Any suggestions?

RebeccaC69 (author)2016-02-06

I'm new to coding and I've followed the steps, done every right (as far as I can tell) but when I get to running the program, the command window keeps telling me '/java is not recognized as an internal or external command, operable program or batch file'

What does this mean?

pawansharma315 made it! (author)2015-10-07

great i made it thanks for you valuable guidance.

sjurrens03 made it! (author)2014-08-21

Great 'ible!

About This Instructable

174,970views

34favorites

License:

More by sniget:How to Make Your First Java Program
Add instructable to: