When you have a Java source file, you can use a Linux terminal to compile it and see what it prints. The javac command turns the source into a class file, and the java command runs that class.
I’ll walk you through compiling and running a Java program in the terminal, including the command for running one source file directly.
TL;DR
To run Java in a Linux terminal, compile the source with javac HelloWorld.java, then launch its class with java HelloWorld.
- Install a JDK so both javac and java are available.
- Run both commands from the directory containing the source file.
- The launcher takes the class name, without .class.
What a Java terminal run does
A Java terminal run turns a source file into a class file, then starts that class on the Java Virtual Machine. The JDK includes the compiler and launcher used for these two steps.
javac reads Java source and writes class files. The java launcher loads a class and calls its main method, which is the program’s entry point.
These steps are for a small command-line program. For broader Java setup, see how to install Java on Debian-based Linux.
What you need before running Java
Use a JDK, not only a runtime package, because compiling requires javac. Check that the shell can find both commands before creating the example.
java --version
javac --version
Both commands should print a version and return to the prompt. The exact version text depends on the installed JDK. If either command is missing, install a JDK using your distribution’s supported package source, then open a new terminal and check again.
How to run a Java program in the terminal
Make a small class with a main method, compile it from its directory, and launch the resulting class by name. I ran the source and commands below in this workspace, and both launch forms printed Hello, World.
Step 1: Create HelloWorld.java
Save this source as HelloWorld.java. The public class name, when present, must match the source filename, including capitalization.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
The main method is where the launcher begins this program. Its body prints one line, which makes the result recognizable.
Step 2: Compile the source with javac
From the directory containing the file, compile the source. The compiler writes HelloWorld.class beside it by default.
javac HelloWorld.java
Successful compilation prints no message and returns to the shell. The generated class file contains bytecode for the Java Virtual Machine.
Step 3: Run the class with java
Pass the class name to the launcher from the same directory.
java HelloWorld
Hello, World
The program printed its message and returned to the shell, and the launcher takes the class name rather than the .class filename.
Step 4: Run a single source file directly
A self-contained source file can run directly with a modern Java launcher. This source-file mode compiles and starts the program in one command, so the intermediate class file is not part of the command sequence. Use it for a quick single-file run.
java HelloWorld.java
Hello, World
I ran this form with the same source file and it printed the same line. Use the two-command form when you want to keep and rerun the compiled class separately.
Fix common Java terminal errors
A command-line failure often means the JDK tool is missing or the launcher cannot locate the class. Check the command and current directory before changing the source.
| Message or symptom | What to check | Next action |
|---|---|---|
| javac: command not found | The JDK compiler is absent or not on PATH. | Install a JDK and open a new shell. |
| Could not find or load main class | Current directory, class name, and class path. | Run from the class-file directory or set -cp. |
| Public class name error | Filename and public class capitalization. | Rename the file or class so the names match. |
The class path tells Java where to look for classes. If you are elsewhere, point it to the directory containing the class file.
java -cp /path/to/classes HelloWorld
Replace the path with the correct class-file directory. Oracle’s java launcher manual documents class launch and class-path options.
Conclusion
Use javac File.java followed by java ClassName when you want to see compilation and execution as separate steps. Use java File.java for a quick run of a self-contained source file.
For compiler options and source-file rules, consult Oracle’s javac reference and the OpenJDK compiler overview.
javac HelloWorld.java && java HelloWorld
Can I run a Java file without compiling it first?
Yes. A modern Java launcher supports source-file mode with a command such as java HelloWorld.java. For a separate compile step, use javac HelloWorld.java and then java HelloWorld.
Do I need a JDK to run a Java program?
Use a JDK for this workflow because it provides javac for compilation as well as the java launcher. A runtime-only installation may not include the compiler.
