Sub topic 8
Compile and Run Hello World
You have written a small Java program. Now you will turn it into a form Java can run, then run it from a terminal.
This chapter assumes that you have installed a Java Development Kit (JDK), checked that its tools are available, and saved HelloWorld.java. You should also know how to open a terminal and move to the folder that contains the file.
The path from file to output
Your Java program starts as source code: text that a person can read and edit. The file HelloWorld.java holds that text.
To run this program in the usual two-step workflow, you first compile it. Compiling means translating source code into another form. The Java compiler, javac, checks the source and creates a class file named HelloWorld.class.
A class file contains bytecode, which is a set of instructions for the Java Virtual Machine (JVM). The JVM is part of the Java runtime, the software that runs Java programs. You start the runtime with the java command.
These are two separate steps. Compiling does not print the greeting. Running the compiled program does.
Check the source file
Your file should be named HelloWorld.java and contain this program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}A class is a named part of a Java program. Here, the class is named HelloWorld. Because it is a public class, its source file must be named HelloWorld.java, with the same capital letters.
The .java ending is a file extension. It helps identify the file as Java source code. The capital letters matter in the class name and command names, so type HelloWorld exactly as shown.
Find the starting point
A method is a named set of instructions in a class. This program has a method named main. When you run the HelloWorld class as a program, Java starts with that method.
You do not need to memorize every part of the method line yet. For now, use it exactly as shown:
publicallows Java to access the method.staticallows Java to start the method without first creating an object from the class. An object is a value made from a class.voidmeans the method does not return a value.String[] argsis a place for words supplied when the program starts. This example does not use them.
Inside main, System.out.println prints a line of text. The quotation marks surround the text to print; they are not part of the output. The semicolon marks the end of the instruction.
Get ready in the terminal
Open a terminal in the folder containing HelloWorld.java. A terminal is a place where you type commands for your computer to carry out. Its working directory is the folder it is currently using.
For this walkthrough, keep the terminal in the source file’s folder for both commands. That lets you use the short file and class names instead of specifying another location.
Type each command exactly as shown, then press Enter. Wait for it to finish before you enter the next one.
Compile the program
In the terminal, enter:
javac HelloWorld.javaThe command has two parts:
javacstarts the Java compiler.HelloWorld.javatells the compiler which source file to read.
The file name is an argument: a piece of information given to a command. Here, the argument includes .java because the compiler needs the source file.
If compilation succeeds, javac normally prints nothing. The terminal simply becomes ready for another command. The compiler creates HelloWorld.class in the same folder as the source file.
You can look in the folder with your computer’s file browser to see the new file. Keep HelloWorld.java too: it is the file you edit when you want to change the program.
What the compiler checks
The compiler checks whether your source follows Java’s rules. For example, it checks the structure of the program and the spelling of Java words. If it finds a problem, it prints an error message instead of successfully compiling the program.
A successful compilation tells you that the compiler could translate the source. It does not, by itself, show the program’s greeting. You still need to run the class.
For this walkthrough, continue only after javac finishes without an error.
Run the compiled class
In the same terminal folder, enter:
java HelloWorldYou should see:
Hello, World!Then the program ends, and the terminal becomes ready for another command.
The java command starts the Java runtime. Its argument, HelloWorld, is the name of the class to run. In this command, do not add .class or .java to the name. You are giving Java the class name, not the name of a file.
The runtime finds the compiled class and starts its main method. The println instruction prints the greeting.
Why the two commands look different
The compiler reads source code, so its argument is the source file name. The runtime starts a class, so its argument is the class name.
| Command | What it uses | What it does |
|---|---|---|
| `javac HelloWorld.java` | The source file | Creates `HelloWorld.class` if compilation succeeds |
| `java HelloWorld` | The compiled class name | Runs the class and prints the greeting |
Notice the capital letters in both commands. HelloWorld, helloworld, and HelloWORLD are different names to Java.
Where Java looks for the class
When you run java HelloWorld, Java looks for the class in places listed on its class path. The class path is a list of locations where Java looks for compiled classes.
Normally, the current working directory is on that list. That is why running the command from the folder containing HelloWorld.class works in a typical setup.
If your class path has been set to something else, you can explicitly tell Java to look in the current folder:
java -cp . HelloWorldHere, -cp supplies a class path, and the dot means the current working directory. The expected output is still Hello, World!. This command does not compile the source file; it runs the existing class file.
Make a change and run it again
A useful way to see the two-step process is to change the greeting.
Open HelloWorld.java in your text editor. A text editor is a program for writing and changing plain text. Change the greeting so the whole file reads:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}Save the file. Saving changes the source file, but it does not automatically update the class file. The existing HelloWorld.class still contains the bytecode from the earlier compilation.
Now compile the saved source again:
javac HelloWorld.javaIf the command finishes without an error, run the class again:
java HelloWorldThis time, the output should be:
Hello, Java!The important sequence is:
- Edit
HelloWorld.java. - Save the file.
- Compile it with
javac HelloWorld.java. - Run the updated class with
java HelloWorld.
You can run java HelloWorld more than once without compiling again, as long as you have not changed the source since the last successful compile. Each run starts the program again.
If you change the source but forget to compile, java HelloWorld can still run the old class file. It will not automatically use your saved change in this workflow.
Read what the terminal shows
For this small program, the terminal shows the line printed by println. It may also show your terminal’s own prompt before and after the command. A prompt is the terminal’s signal that it is ready for you to type.
The prompt is not part of the Java program’s output. Its appearance can vary by computer, so it is not shown in the expected-output examples.
The compiler and runtime can also print messages when they cannot complete a command. Those messages are different from the greeting produced by your program. If you see a message instead of the expected result:
- Check which command produced it:
javacorjava. - Read the message before trying again.
- If compilation failed, correct and save the source, then compile again before running.
The next chapter covers common setup and compilation problems in more detail.
Review the workflow
The source file and class file have different jobs:
HelloWorld.javais the readable source you write and edit.HelloWorld.classis the compiled bytecode Java runs.javac HelloWorld.javaupdates the class file from the saved source.java HelloWorldruns the compiled class.
Keep the source and class names straight, and remember to compile after every saved change that you want to run.
Exercises
Exercise
You saved a change to HelloWorld.java. What two commands should you run, and in what order?
Show answer
Run javac HelloWorld.java first. If it finishes without an error, run java HelloWorld.
Exercise
Change the greeting to Good morning!, save the source, compile it, and run it. What should appear?
Show answer
The program should print Good morning! on one line. Be sure to change the text inside the quotation marks, save the file, and compile before running.
Exercise
You change the source greeting but run java HelloWorld without compiling again. Why might you see the old greeting?
Show answer
The java command runs the existing class file. That file still contains the bytecode from the last successful compilation, not the new source change.