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. Visual Studio Code configured with the Extension Pack for Java
  3. a cup of Java — coffee! Get it?

Test your code at every step!

2. Create & compile your first class

VSCode’s Integrated Terminal is one of the many features which makes VSCode so popular amongst developers. Rather than having to run a separate cmd prompt to compile and run your code, you can do all that in the Terminal.

  1. Create a folder named HelloWorld
  2. Open VSCode
  3. Open the HelloWorld folder
  4. File >> New File >> New Java File… Java Class
  5. Open Windows Explorer in the HelloWorld folder — you will see the file HelloWorld.java
  6. Open in Integrated Terminal
  7. Compile your code: javac HelloWorld.java
  8. 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. 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 Terminal: 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. 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. Compile your code (type the following at the Terminal: javac HelloWorld.java)
  2. Run your program (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.