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.
In this tutorial:
Required knowledge:
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:
- a laptop running Windows 10
- Visual Studio Code configured with the Extension Pack for Java
- 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.
- Create a folder named HelloWorld
- Open VSCode
- Open the HelloWorld folder
- File >> New File >> New Java File… Java Class
- Open Windows Explorer in the HelloWorld folder — you will see the file HelloWorld.java
- Open in Integrated Terminal
- Compile your code: javac HelloWorld.java
- 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)
- Now type & enter the following command at the prompt: java HelloWorld
- 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
- Add the following code “around” your existing code:
public static void main(String[] arguments) {}
- 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:
- 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!
- Type the following command at the prompt: java HelloWorld
Awesome: it does nothing – but at least there are no errors!
6. Do something!
- 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!"); } }
- Compile your code (type the following at the Terminal: javac HelloWorld.java)
- 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.