You Wrote the Code, Now Make It Run
You’ve just typed out your first Java program, saved it as MyProgram.java, and compiled it successfully. A new MyProgram.class file now sits in your directory. You feel a surge of accomplishment—until you stare at the file and realize you’re not quite sure what to do next. How do you actually run it?
This moment is a common rite of passage for every Java developer. The transition from writing code to executing it is a fundamental step, yet the process can seem opaque if you’re new to the command line or the intricacies of the Java Virtual Machine (JVM). Unlike double-clicking an .exe file, running a Java class requires a specific command that tells the Java runtime exactly where to find your code and where to begin.
This guide will walk you through the exact steps to run a Java class file, whether you’re using a simple command prompt, a complex project structure, or a modern Integrated Development Environment (IDE). We’ll cover the prerequisites, the core command, and how to troubleshoot the most common errors you’ll encounter.
What You Need Before You Can Run Anything
Before you can issue the run command, you must have two critical components installed and correctly configured on your system: the Java Development Kit (JDK) and a working command-line interface.
Installing the Correct Java Version
The Java Runtime Environment (JRE) alone is not enough to compile code, but it is sufficient to run already-compiled .class files. However, for full development, you need the JDK. To check what’s installed, open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and type:
java -version
javac -version
The first command checks for the Java runtime, the second for the Java compiler. If either command returns an error like “‘java’ is not recognized as an internal or external command,” the Java binaries are not in your system’s PATH environment variable.
You must install a JDK. We recommend downloading the latest Long-Term Support (LTS) version from adoptium.net (Eclipse Temurin) or oracle.com. During installation on Windows, pay attention to the option to “Add to PATH.” If you missed it, or are on macOS/Linux, you’ll need to set the PATH manually.
Understanding the Classpath
The single most important concept for running Java classes is the classpath. Think of it as the list of directories and JAR files where the JVM goes searching for your .class files. By default, the JVM looks in the current directory. If your class is elsewhere, or depends on external libraries, you must specify the classpath explicitly.
Failing to set the classpath correctly is the root cause of the most common error: “Error: Could not find or load main class.”
The Core Command: Running Your Class File
Let’s start with the simplest scenario. You have a file named HelloWorld.java in a directory called C:\MyJavaProject or ~/MyJavaProject.
The source code inside is:
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
First, you must compile it. Navigate to that directory in your terminal and run:
javac HelloWorld.java
This produces HelloWorld.class. Now, to run it, the command is:
java HelloWorld
Do not add the .class extension. The `java` command expects the class name. The JVM uses the classpath (currently your working directory) to find the HelloWorld.class file, loads it, and executes its `main` method.
Running from a Different Directory
What if you’re not in the same directory as your .class file? You must specify the classpath. Suppose your HelloWorld.class file is located at /home/user/project/bin.
You can run it from anywhere by pointing the classpath to that directory:
java -cp /home/user/project/bin HelloWorld
The `-cp` flag (or `-classpath`) sets the search path. The JVM will now look in /home/user/project/bin for the HelloWorld class.
Running a Class Inside a Package
Java applications use packages to organize code. If your class declares a package, running it requires an extra consideration. Let’s say your file is:
package com.example;
public class Greeter {
public static void main(String[] args) {
System.out.println(“Greetings!”);
}
}
After compiling with `javac Greeter.java`, you will have a file Greeter.class. The crucial part is that it must be placed in a directory structure that matches the package name: com/example/. So your project layout should be:
/project
/com
/example
Greeter.class
To run it, you navigate to the root directory that contains the top of the package hierarchy (the `/project` folder in this case) and run:
java -cp . com.example.Greeter
Notice you use the fully qualified class name: `com.example.Greeter`. The classpath is set to the current directory (`.`), and the JVM will look for a `com/example/Greeter.class` file within it.
Running Within an Integrated Development Environment
While the command line is essential for understanding, most development happens in an IDE like IntelliJ IDEA, Eclipse, or VS Code. These tools automate the compile-and-run cycle.
IntelliJ IDEA
After creating a project and adding your Java class with a main method, IntelliJ adds a small green play icon (▶) in the gutter next to the method declaration. Clicking this icon gives you the option “Run ‘HelloWorld.main()'”. This action automatically compiles the code (if necessary) and executes it. The run configuration, which includes classpath and JVM arguments, is managed for you in the background.
Eclipse
In Eclipse, you can right-click on the Java file in the Project Explorer, or click anywhere inside the editor of the class containing the main method. Select “Run As” > “Java Application”. The console view at the bottom will show the program output.
Visual Studio Code with Java Extensions
With the “Extension Pack for Java” installed, VS Code detects classes with a main method. You’ll see “Run” and “Debug” links above the main method. Clicking “Run” executes the class. You can also use the Run view (Ctrl+Shift+D) to manage multiple run configurations.
The key advantage of an IDE is that it handles directory structure, classpath, and library dependencies defined in your build file (like Maven’s pom.xml or Gradle’s build.gradle) automatically.
Troubleshooting Common “Could Not Run” Errors
Even with the correct command, things can go wrong. Here’s how to diagnose and fix the most frequent errors.
Error: Could not find or load main class
This is the universal Java runtime error. It means the JVM searched the classpath and could not locate a class with the exact name you provided that also contains a valid `public static void main(String[] args)` method.
– Check the class name for typos. Is it `HelloWorld` or `Helloword`?
– Are you in the wrong directory? If not using `-cp`, ensure your terminal’s working directory is the one containing the .class file.
– For packaged classes, are you in the correct root directory and using the fully qualified name?
– Did compilation actually succeed? Check that the .class file exists where you expect it.
– Is the `main` method signature exactly correct? `public static void main(String[] args)`.
Error: Main method not found in class
This is more specific. The JVM found the class, but the class doesn’t have a valid main method. Verify the method is `public`, `static`, returns `void`, is named `main`, and accepts a single parameter of type `String[]` (or `String…`).
Dealing with External JAR Dependencies
If your program uses a library like Gson or Apache Commons, you must include the JAR file in the classpath. Separate multiple paths with a semicolon (;) on Windows or a colon (:) on macOS/Linux.
java -cp “.;./libs/gson-2.10.jar” com.example.MyApp
This tells the JVM to look in the current directory and the gson JAR file for any required classes.
Alternative Methods and Advanced Execution
Beyond the basic `java` command, there are other ways to launch Java code, each suited for different stages of development and deployment.
Using the Java Package Tool (jpackage)
For distributing your application to users who shouldn’t need a command line, you can create native installers. The `jpackage` tool (available in JDK 14+) bundles your JAR files, a JVM, and creates a .exe, .dmg, or .deb/.rpm installer. The user can then run your app like any other desktop program.
Creating Executable JAR Files
A more common distribution method is the executable JAR. You package all your .class files and resources into a single .jar archive. You specify the main class in a manifest file, and then users can run it with:
java -jar MyApplication.jar
This is a clean, single-command approach that encapsulates the classpath within the JAR. You can create such a JAR using the `jar` command-line tool or, more commonly, through your IDE or build system like Maven or Gradle.
Running with Custom JVM Arguments
The `java` command accepts numerous flags to control the JVM’s behavior. For example, to set the initial heap size, you would use:
java -Xms256m -Xmx1024m MyApp
Other useful flags include `-D` to set system properties (`-Dconfig.file=app.properties`) and `-verbose:class` to log class loading for debugging.
Your Next Steps Toward Java Mastery
Successfully running a Java class file is the first step in a much larger journey. The command line is your foundation—understanding it makes you a more powerful and debug-capable developer. From here, focus on automating your build process. Learn a build tool like Maven or Gradle. These tools standardize how projects are compiled, tested, packaged, and run, making the `javac` and `java` commands you just mastered part of a single, repeatable workflow.
Experiment with creating a multi-module project, where running the main class depends on a complex inter-project classpath managed by your build tool. Finally, explore creating a proper executable JAR or a native image using GraalVM. Each of these steps builds directly on the fundamental skill of knowing exactly how the JVM finds and executes your code. Now that you can run your class, the real building begins.