To run a Java file from the terminal, compile it with javac and then execute it with java using the class name (without the .java extension). First, ensure the Java Development Kit (JDK) is installed and the file is saved with a .java extension matching the public class name.

What You'll Need

  • A Java source file (e.g., HelloWorld.java)
  • Java Development Kit (JDK) installed (version 8 or later)
  • Access to a terminal or command prompt
  • Basic knowledge of navigating directories in the terminal

Step-by-Step

  1. Open your terminal application (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows).
  2. Navigate to the directory containing your Java file using the cd command. For example: cd Documents/JavaProjects.
  3. Verify the file is present by listing directory contents: ls (macOS/Linux) or dir (Windows). You should see your .java file.
  4. Compile the Java file using the javac command. Type: javac HelloWorld.java (replace HelloWorld with your file name). This generates a .class file if there are no syntax errors.
  5. If compilation succeeds with no error messages, run the program using the java command and the class name: java HelloWorld. Do not include the .class extension.
  6. Observe the output in the terminal. For a basic Hello World program, you should see "Hello, World!" printed.

Common Mistakes

  • Forgetting to compile before running: Attempting to run java HelloWorld.java will fail because the JVM expects bytecode, not source code.
  • Using the file name with .java or .class extension in the java command: You must use only the class name (e.g., java HelloWorld, not java HelloWorld.class).
  • Mismatch between the public class name and the file name: If your class is public and named HelloWorld, the file must be exactly HelloWorld.java. A mismatch causes a compilation error.
  • Running from the wrong directory: If you compile in one folder and run from another, the JVM may not find the .class file. Always run from the directory containing the .class file or include the path in the classpath.
  • Missing JDK installation: Only having the JRE (Java Runtime Environment) installed means javac is unavailable. You need the JDK for compilation.

Tips

  • Check your JDK installation by running javac -version and java -version in the terminal. Both should return version numbers without errors.
  • If you're learning Java, use an IDE like IntelliJ IDEA, Eclipse, or VS Code for better error highlighting and automation, but knowing the terminal method is essential for understanding the build process.
  • For packages, compile and run from the root directory of your source structure. For example, if your class is in package com.example.app, place the file in com/example/app/HelloWorld.java, then run from the parent directory of com using: java com.example.app.HelloWorld.
  • To avoid typing long commands repeatedly, create a simple shell script or batch file that compiles and runs your Java file with one command.
  • Enable assertions during development with java -ea HelloWorld if your code uses assert statements.

Troubleshooting

  • Error: 'javac' is not recognized as an internal or external command This means the JDK is not installed or not in your system's PATH. Install the JDK from Oracle's official website or use OpenJDK. After installation, restart the terminal and verify with javac -version.
  • Error: Could not find or load main class HelloWorld This occurs when the JVM cannot locate the .class file. Ensure you are in the correct directory and that the .class file was generated. Recompile if needed. If using packages, include the full package path in the class name.
  • Error: HelloWorld.java:1: error: class HelloWorld is public, should be declared in a file named HelloWorld.java Your file name does not match the public class name. Rename the file to exactly match the class name (case-sensitive) or change the class name to match the file.
  • Error: Exception in thread "main" java.lang.NoClassDefFoundError This usually indicates a typo in the class name when running or an incorrect classpath. Double-check the spelling and case of the class name in the java command.
  • No output appears after running Verify your code actually contains a print statement (e.g., System.out.println()) and that the main method is correctly defined as public static void main(String[] args).

Frequently Asked Questions

Do I need to install anything to run Java from the terminal?

Yes, you need the Java Development Kit (JDK), not just the JRE. The JDK includes the javac compiler and other development tools.

Can I run a Java file without compiling it first?

No, Java source code must be compiled into bytecode (.class file) before the JVM can execute it. There is no direct way to run .java files like a script.

What if my Java file has multiple classes?

Only one public class per file is allowed, and the file must match that class's name. You can have multiple non-public (package-private) classes in the same file, but only the public class (or the file name if no public class) is used as the entry point.

How do I run a Java file that takes command-line arguments?

After the class name in the java command, add the arguments separated by spaces. For example: java HelloWorld arg1 arg2. Access them in your code via the args array in main(String[] args).

Is there a difference between running Java on Windows, macOS, and Linux?

The javac and java commands are the same across platforms. The main differences are in file path syntax (e.g., using backslashes on Windows vs. forward slashes elsewhere) and terminal applications, but the core process remains identical.

Why do I get a security warning when running Java from the terminal?

You typically won't get security warnings when running locally compiled Java code from the terminal. Warnings usually appear only when running Java applets or Web Start applications from browsers, which are largely obsolete.