To run a Java program from the terminal, you use two commands: javac to compile the .java source file into a .class bytecode file, and java to execute that class. This guide walks you through the exact steps, common pitfalls, and troubleshooting for both commands on Windows, macOS, and Linux. You need a working Java Development Kit (JDK) installed, not just the Java Runtime Environment (JRE), because javac ships only with the JDK.
What You'll Need
- A terminal or command prompt (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows).
- Java Development Kit (JDK) version 8 or newer (11 or 17 are common LTS versions). Check with
java -versionandjavac -version. - A text editor to write your Java source file (any plain text editor works; save the file with
.javaextension). - Basic knowledge of navigating directories in your terminal (
cdto change directory,lsordirto list files).
Step-by-Step
- Verify Java is installed. Open your terminal and run
java -version. If you see a version number, Java is installed. Then runjavac -versionto confirm the compiler is available. Ifjavacis not found, install a JDK (not just a JRE) from the official Oracle or OpenJDK builds. - Create a Java source file. Use any text editor to write a simple program. For example, create a file named
Hello.javawith the following content:public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); } }Save the file with the exact nameHello.java– the class name must match the file name. - Navigate to the directory containing your file. Use the
cdcommand to change into the folder whereHello.javais saved. For example,cd /path/to/your/java/fileson macOS/Linux orcd C:\path\to\your\java\fileson Windows. You can typels(macOS/Linux) ordir(Windows) to confirm the file is there. - Compile the Java file. Run
javac Hello.java. If the compilation succeeds, you will see no output, and a new file namedHello.classwill appear in the same directory. This is the bytecode file that the Java Virtual Machine (JVM) can execute. - Run the compiled class. Execute
java Hello(without the.classextension). The program should printHello, world!to the terminal. If you get a “Could not find or load main class” error, see the Troubleshooting section below. - Handle packages (optional). If your class is in a package, for example
package com.example;, you must create the directory structure matching the package (e.g.,com/example/) and compile from the parent directory of the package root. Then run with the fully qualified name:java com.example.Hello. - Pass command-line arguments. To pass arguments to your program, add them after the class name:
java Hello arg1 arg2. Inside your Java code, they are available in theString[] argsparameter ofmain.
Common Mistakes
- Forgetting the
.javaextension when compiling.javac Hellowill fail; you must typejavac Hello.java. - Adding
.classwhen running.java Hello.classwill cause a “Could not find or load main class” error. Usejava Hello. - Class name mismatch. If your file is
Hello.javabut the class inside ishello(lowercase), the compiler will error. Java is case-sensitive, and the public class name must exactly match the file name. - Running from the wrong directory. If you run
java Hellofrom a different folder, the JVM won’t find the.classfile. Alwayscdto the directory containing the compiled class, or use the classpath option. - Using a JRE instead of a JDK. If
javacis not recognized, you likely have only the JRE installed. Install a full JDK. - Path issues on Windows. After installing the JDK, you may need to add the
bindirectory (e.g.,C:\Program Files\Java\jdk-17\bin) to your system PATH environment variable, or the terminal won’t findjavacorjava.
Tips
- Use the classpath flag for multiple files. If your program depends on classes in another directory, compile with
javac -cp .;lib\* Hello.java(Windows) orjavac -cp .:lib/* Hello.java(macOS/Linux) to include external JARs. - Compile all Java files at once. In a project with many files, run
javac *.javato compile every.javafile in the current directory. This is faster than compiling one by one. - Use the
-dflag to organise output.javac -d bin Hello.javaplaces the.classfile into abindirectory, keeping your source and compiled files separate. Then run withjava -cp bin Hello. - Set the classpath for running. If your classes are not in the current directory, use
java -cp .;lib\* Hello(Windows) orjava -cp .:lib/* Hello(macOS/Linux) to tell the JVM where to look. - Check your Java version. Some newer features require a specific JDK version. If you get a “Unsupported class version” error, your compiler is newer than your runtime, or vice versa. Match the versions.
- Use an IDE for large projects. For big applications, an IDE like IntelliJ IDEA or Eclipse manages compilation and classpath automatically. But the terminal commands still work for simple scripts and learning.
Troubleshooting
If your command does not work as expected, follow these checks in order.
- “javac is not recognized” or “command not found”. This means the JDK is not installed or not in your PATH. Install a JDK from the official Oracle website or an OpenJDK distribution. After installation, restart your terminal. On Windows, you may need to update the PATH environment variable manually.
- “Could not find or load main class”. This usually means the JVM cannot locate your
.classfile. Check that you are in the correct directory (uselsordirto see ifHello.classexists). Ensure you typed the class name exactly as defined, without the.classextension. If your class is in a package, you must run it from the parent directory of the package root and use the fully qualified name. - Compilation errors like “cannot find symbol”. This often happens when you reference a class that hasn’t been compiled yet. Compile all source files together with
javac *.javaor specify the classpath to the source files. - “UnsupportedClassVersionError”. Your compiled class was created with a newer JDK than the one used to run it. Either run with a newer JRE or recompile with an older JDK (or use
--releaseflag, e.g.,javac --release 8 Hello.java). - “Error: Could not create the Java Virtual Machine”. This is often due to insufficient memory settings. If you have set
JAVA_OPTSor_JAVA_OPTIONSwith a too-large heap size, reduce it. Otherwise, check your system’s available memory. - File encoding issues. If your source file contains non-ASCII characters and you get encoding errors, compile with
javac -encoding UTF-8 Hello.javato specify the character encoding.
Frequently Asked Questions
What is the difference between java and javac?
javac is the Java compiler; it converts your .java source code into .class bytecode. java is the launcher that starts the Java Virtual Machine and runs the compiled bytecode. You need both for development, but only java is needed to run pre-compiled programs (if you have a JRE).
Do I need to compile every time I change my code?
Yes, javac must be run again after any change to the source file. The JVM runs the .class file, which is a snapshot of your code at compile time. Some IDEs auto-compile, but from the terminal you must manually run javac.
Why do I get “class Hello is public, should be declared in a file named Hello.java”?
This error occurs when the public class name does not match the file name. In Java, a public class must be in a file with the same name (case-sensitive) and the .java extension. Rename your file or your class so they match.
Can I run a Java program from a different directory?
Yes, use the -cp (classpath) option. For example, if your .class file is in /home/me/classes, run java -cp /home/me/classes Hello. Similarly, you can compile from anywhere by specifying the full path: javac /home/me/Hello.java.
How do I set the PATH for Java on Windows?
After installing the JDK, go to System Properties > Environment Variables, find the Path variable, and add the JDK’s bin directory (e.g., C:\Program Files\Java\jdk-17\bin) at the beginning. Then open a new Command Prompt and test with java -version.
What does “package system does not exist” mean?
This error usually appears when you try to compile a file that uses a package but the package structure is incorrect. For example, if you have package com.example;, the file must be inside com/example/ directory, and you must compile from the parent of com (e.g., javac com/example/Hello.java).
Is there a way to run a Java program without compiling?
No, Java is a compiled language. However, you can use the Java Shell (JShell) for interactive experimentation, but that is not for running full programs. For a single-file source-code program (Java 11+), you can run java Hello.java directly, which compiles and runs in one step, but it still compiles under the hood.