MisterFoxOnline avatar

I am still busy with this post. Be sure to come back soon to see the final version — and don’t be shy to post questions in the Comments — especially if I’ve missed something!

In the Setting up a computer for Java development tutorial I noted my disappointment at the lack of anything cool happening. While I understand that for most people having a computer program simply results in the message “Hello World!” appearing somewhere on the screen is maybe an anticlimax, for us geeks it is sometimes the equivalent of summiting Mount Everest. So I am going to work towards reaching that monumental moment as quickly as possible and then backtrack later to do the actual explaining part.

1. Set-up

Read Setting up a computer for Java development if your computer is not ready for the task at hand!

What I used in this tutorial:

  1. a laptop running Windows 10
  2. my favourite text editor: Notepad++
  3. the JDK
  4. some very basic knowledge of the Windows Command Prompt
  5. a cup of Java — coffee! Get it?

Test your code at every step!

2. Create & compile your first class

  1. Open the Windows Command Prompt
    • Windows Key on the keyboard. + R
    • type cmd
    • Click the OK button
  2. Open Notepad++
  3. Save the default blank file that opens as HelloWorld.java in the location where your command prompt is open at
  4. Open Windows Explorer at the same location — you will see the file HelloWorld.java
  5. Type the following command at the prompt: javac HelloWorld.java
  6. The blank file will compile with no errors and if you look in your folder, in addition to your HelloWorld.java file you will see the file HelloWorld.class

3. Run your first program (& experience your first error)

  1. Now type & enter the following command at the prompt: java HelloWorld
  2. An error message will be output, but at least your “program” is doing something, right?

The name of the program in Line 1 must match the name of the JAVA file.

4. Fix your error

  1. Go back to Notepad++ and add the following code “around” your existing code: public static void main(String[] arguments) {}
  2. Your “program” now looks as follows:
class HelloWorld {
	public static void main(String[] arguments) {
		// this is just a comment
	}	
}

Visit Windows Command Line basics for a basic Windows CLI primer!

Your program will now compile successfully:

  1. Type the following command at the prompt: javac HelloWorld.java

5. Run

Your program has compiled successfully and now it is finally time to run it!

  1. Type the following command at the prompt: java HelloWorld

Awesome: it does nothing – but at least there are no errors!

6. Do something!

  1. Switch back to Notepad++ and add the line of code: system.out.print("Hello World!");
class HelloWorld {
	public static void main(String[] arguments) {
		// this is just a comment
		System.out.print("Hello World!");
	}	
}
  1. Type the following at the command prompt: javac HelloWorld.java
  2. Type the following at the command prompt: java HelloWorld

7. Next steps

Head on over to the post Sending arguments to a Java application.

By MisterFoxOnline

Mister Fox AKA @MisterFoxOnline is an ICT, IT and CAT Teacher who has just finished training as a Young Engineers instructor. He has a passion for technology and loves to find solutions to problems using the skills he has learned in the course of his IT career.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.