Sub topic 8
Compile and Run Hello World
You have saved HelloWorld.java and checked that the Java tools work in your terminal. A terminal is a window where you enter text commands. For this chapter, open it in the folder that contains HelloWorld.java.
The example is in the default package, which means it has no package line. It defines a class named HelloWorld and prints Hello, World!. A class is a named part of a Java program.
From source file to running program
The .java file is a source file: it contains the Java code you wrote. To run it with the steps in this chapter, you first compile it.
A compiler is a tool that translates source code into another form. The Java compiler, javac, produces bytecode: instructions that the Java runtime can execute. The runtime is the software that runs a Java program.
Compile the source file
In the terminal, enter:
javac HelloWorld.javaPress Enter. This tells javac to compile the file named HelloWorld.java.
If compilation succeeds, the compiler normally prints nothing. The terminal simply shows a new prompt, the place where you can enter another command. The compiler creates a file named HelloWorld.class in the same folder.
A .class file contains the compiled bytecode. Keep the original .java file too: it is the file you can read and edit.
If the compiler prints an error instead, read the message and correct the source file before continuing. An error can leave an older .class file in the folder, so running it may not test your latest changes. The troubleshooting chapter covers common errors.
Run the class
After a successful compile, enter:
java HelloWorldYou should see:
Hello, World!
The java command starts the Java runtime. The runtime uses a component called the Java Virtual Machine, or JVM, to execute the bytecode in the class file.
Notice the difference between the two commands:
javac HelloWorld.javatakes the source file name, including.java.java HelloWorldtakes the class name, without.javaor.class.
With the default class path, the runtime looks for HelloWorld.class in the current folder. The class path is the set of places where Java looks for classes. Use the same capitalization shown above: HelloWorld is not the same name as helloworld.
Where the program starts
The runtime starts this program at its main method. A method is a named set of instructions. The main method is the entry point: the place where Java begins running this program.
Inside main, the example uses System.out.println to print the greeting. The text inside the quotation marks is what appears in the terminal. The command then ends, and the terminal is ready for another command.
Change the greeting
Try the compile-and-run cycle once more:
- Open
HelloWorld.javain your text editor. - Change the text
Hello, World!toHello, Java!. Keep the quotation marks around it. - Save the file.
- Compile it again. If compilation succeeds, run it again.
Use these commands:
javac HelloWorld.java
java HelloWorldThe output should now be Hello, Java!.
Editing the .java file does not update the .class file by itself. In this workflow, compile after each saved change so that the runtime uses the new instructions. If compilation reports an error, fix it and compile again before running.
Exercise
You change the greeting in HelloWorld.java and save it. Why must you run javac HelloWorld.java before java HelloWorld?
Show answer
The edit changes the source file, not the existing class file. Compiling creates updated bytecode for the runtime to execute.