Sub topic 9
Running and compiling a compact source file
Prerequisites and the two ways to run
Assume you know basic Java methods and control flow, the traditional main method, and the command line. This chapter uses Java 25, where compact source files and instance main methods are final features.
A compact source file contains top-level methods and fields without an explicit class declaration. For example, save this as Hello.java:
void main() {
IO.println("Hello, world!");
}The file still defines a Java program. The compiler supplies an implicitly declared class around its members.
You can run the source file directly:
java Hello.javaThe java launcher reads the source file, compiles it in memory, finds a launchable main method, and runs the program. This mode is called source-file mode. It is useful for small programs, experiments, and classroom exercises because it avoids a separate compilation command.
You can also compile first:
javac Hello.java
java HelloThe javac compiler creates a class file. The second command starts the generated class by its class name, not by its filename with the .java extension.
| Goal | Command | Result |
|---|---|---|
| Run source directly | `java Hello.java` | Compiles and runs the source |
| Compile for later use | `javac Hello.java` | Creates a class file |
| Run compiled output | `java Hello` | Loads and runs the class |
What each command expects
In source-file mode, give java the path to the source file:
java path/to/Hello.javaThe launcher does not treat Hello.java as a class name. The .java suffix tells it to use source-file mode.
After compilation, omit the suffix:
javac path/to/Hello.java
java -cp path/to HelloThe -cp option sets the class path, which tells the launcher where to search for class files. If you run both commands from the same directory, the shorter form works:
javac Hello.java
java HelloA compact source file can use an instance main method. The launcher creates an instance of the implicit class before calling it:
int runs;
void main() {
runs++;
IO.println("Run number " + runs);
}Each execution creates a new instance, so runs starts at zero for each separate launch.
The same source can be expanded into an explicit class later:
public class Hello {
int runs;
void main() {
runs++;
System.out.println("Run number " + runs);
}
}The explicit version uses System.out here, but IO.println also works in Java 25. The important change is the class declaration. The behavior of the instance main method remains the same.
Follow the launch process
Common errors
- Writing
java Hellobefore compiling causes a class-not-found error unless an existingHello.classis available. - Writing
java Hello.javaafter compilation still uses source-file mode. It does not run the class file directly. - Running from the wrong directory can make either the source file or class file unavailable.
- A compact source file cannot contain an explicit top-level class declaration for the implicit class itself. Add a named class only when it is a helper type or convert the whole file to an explicit class.
Exercise
Which commands compile and run a compact source file named Hello.java using a generated class file?
Show answer
Run javac Hello.java, then run java Hello.