Hello World
The Aims
In this exercise, you will create and run your first Java application. This will be done using Notepad to edit the file, and we'll compile and run the application from the command line.
Checking Java is installed
-
Open a command prompt - click on the search bar and type in
cmd. -
Type the following commands into the command prompt to find out which version of Java and the Java compiler are installed on your computer:
java -versionjavac -version
Create a Java application
We are going to do this, simply to check that the Java Development Kit and the Java Virtual Machine are installed and working. Don't worry about understanding the content of the file we are creating yet!
-
Create a new folder on your computer to store the files. Name the folder "HelloWorld".
-
Using Notepad, create a new text file in the HelloWorld folder called "HelloWorld.java"
-
Edit the file so that the contents are as follows:
class HelloWorld {public static void main(String[] args) {System.out.println("Hello World!");}} -
From the command prompt, check that the file is called HelloWorld.java, and not HelloWorld.java.txt (Notepad might create the file with a .txt extension.) To do this enter the following command:
dir -
If the file is called HelloWorld.java.txt, rename it with this command:
rename HelloWorld.java.txt HelloWorld.java -
Compile the file to Java bytecode by running this command:
javac HelloWorld.java -
Check the bytecode file has been created by running:
dir -
Run the application with this command:
java HelloWorld
You should see the words "Hello World!" displayed on the console. Congratulations - you have just written, compiled and executed your first Java application!