How can I run a Java program is a common question for beginners stepping into the world of object‑oriented programming. Java’s “write once, run anywhere” promise relies on a two‑step process: first you compile source code into bytecode, then the Java Virtual Machine (JVM) executes that bytecode on any platform that has a compatible runtime installed. Understanding each step, the tools involved, and typical pitfalls makes the experience smooth and helps you avoid frustration when your code refuses to launch. Below is a detailed, step‑by‑step guide that covers everything from setting up the JDK to running Java applications from the command line and inside popular Integrated Development Environments (IDEs).
Prerequisites: Installing the Java Development Kit (JDK)
Before you can compile or run any Java code, you need the Java Development Kit (JDK). The JDK includes the javac compiler, the java launcher, and essential libraries Easy to understand, harder to ignore..
- Download the JDK – Visit the official Oracle JDK page or adopt an OpenJDK build (e.g., Adoptium, Amazon Corretto). Choose the version that matches your operating system (Windows, macOS, Linux) and architecture (64‑bit is standard today).
- Run the installer – Follow the wizard; on Windows you can accept the default location, on macOS drag the JDK to /Library/Java/JavaVirtualMachines, and on Linux use your package manager (
sudo apt install openjdk-21-jdkfor Ubuntu, for example). - Set the PATH environment variable – Ensure the
bindirectory of the JDK is added to your system’sPATH. This lets you calljavacandjavafrom any terminal.- Windows: System Properties → Advanced → Environment Variables → Path → Edit → New →
C:\Program Files\Java\jdk-21\bin - macOS/Linux: Add
export PATH=$PATH:/path/to/jdk-21/binto~/.bashrcor~/.zshrc.
- Windows: System Properties → Advanced → Environment Variables → Path → Edit → New →
- Verify the installation – Open a new terminal and run:
Both commands should display version numbers without errors.java -version javac -version
Writing a Simple Java Program
Let’s create a classic “HelloWorld” application to illustrate the workflow.
- Create a folder for your project, e.g.,
~/java-demo. - Create a file named
HelloWorld.javainside that folder. - Enter the following code (note that the class name must match the filename exactly, case‑sensitive):
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }public classdeclares a class accessible from anywhere.public static void main(String[] args)is the entry point the JVM looks for.System.out.printlnprints text to the console.
Compiling Java Code
Compilation translates human‑readable .java source into platform‑independent bytecode stored in a .class file Worth knowing..
- handle to the project directory in your terminal:
cd ~/java-demo - Run the javac compiler:
If there are no syntax errors, the command finishes silently and producesjavac HelloWorld.javaHelloWorld.classin the same directory.- Tip: Use
-d <output-dir>to place.classfiles in a separate directory, keeping source folders clean:javac -d out HelloWorld.java
- Tip: Use
Running the Java Program
Now that bytecode exists, the JVM can execute it Not complicated — just consistent..
- Launch the program with the
javacommand, specifying the class name without the.classextension:
You should see:java HelloWorldHello, World! - If you used an output directory (
outin the previous step), include the classpath (-cp) flag:java -cp out HelloWorld
Understanding the Classpath
The classpath tells the JVM where to look for user‑defined classes and packages. Think about it: ). In practice, when you store . By default, it includes the current directory (.class files elsewhere, you must explicitly add that location Easy to understand, harder to ignore. Less friction, more output..
Running Java Programs Inside an IDE
While the command line offers full control, most developers prefer an IDE for productivity. Below are quick start steps for three popular IDEs.
Eclipse IDE
- Install Eclipse – Download the “Eclipse IDE for Java Developers” package from eclipse.org.
- Create a Java Project –
File → New → Java Project. Name it (e.g.,HelloWorldProject). - Add a Class – Right‑click the
srcfolder →New → Class. Name itHelloWorldand check the box forpublic static void main(String[] args). Eclipse will generate the skeleton. - Run – Right‑click the class →
Run As → Java Application. The Console view prints the output.
IntelliJ IDEA
- Install IntelliJ IDEA – Choose the Community Edition (free) from jetbrains.com.
- New Project –
File → New → Project. Select Java, specify JDK, and finish. - Create a Class – Right‑click the
srcfolder →New → Java Class. Name itHelloWorld. Paste the main method. - Run – Click the green arrow next to the class declaration or press
Shift+F10. Output appears in the Run tool window.
NetBeans IDE
- Install NetBeans – Download from netbeans.apache.org.
- New Project –
File → New Project → Java → Java Application. Name it and ensure the “Create Main Class” box is checked (you can rename it toHelloWorld). - Edit the Source – The IDE opens
HelloWorld.java; replace the generated code with your own if needed. - Run – Press
F6or click the Run button. Output shows in the Output window.
Common Errors and How to Fix Them
Even experienced developers encounter hiccups. Below are frequent issues and their remedies.
| Error Message | Likely Cause | Solution |
|---|---|---|
Error: Could not find or load main class HelloWorld |
The JVM cannot locate the `.On the flip side, | Ensure you are in the directory containing HelloWorld. class file (wrong class name or classpath). classor use-cp` to point to it. Verify the class name matches the filename exactly (case‑sensitive). |
javac: file not found: HelloWorld.On the flip side, java | The compiler cannot locate the source file. In practice, | Run javac from the directory containing HelloWorld. Here's the thing — java or provide the full/relative path to the file. |
| error: class HelloWorld is public, should be declared in a file named HelloWorld.java | Filename and public class name mismatch (case‑sensitive). | Rename the file to exactly match the public class name (HelloWorld.Practically speaking, java). Worth adding: |
| Error: Main method not found in class HelloWorld | Missing or incorrectly signed main method. On the flip side, | Ensure the method signature is exactly public static void main(String[] args). Because of that, |
| UnsupportedClassVersionError | The . class file was compiled with a newer Java version than the runtime. | Compile with --release flag targeting your runtime (e.g.Here's the thing — , javac --release 17 HelloWorld. java) or run with a newer JRE. Day to day, |
| NoClassDefFoundError (at runtime) | A dependency is on the compile‑time classpath but missing at runtime. | Add the missing JAR or directory to the runtime classpath via -cp or your IDE’s run configuration.
Packaging for Distribution
Once your program works, you’ll often want to share it without requiring recipients to manage classpaths manually.
Executable JAR Files
- Create a Manifest – Write a
MANIFEST.MFfile specifying the entry point:
(Include a blank line at the end.)Main-Class: HelloWorld - Package – Run:
jar cfm HelloWorld.jar MANIFEST.MF HelloWorld.class - Run Anywhere – Users execute it with a double‑click or:
java -jar HelloWorld.jar
Native Images with GraalVM (Optional)
For startup‑critical or containerized workloads, compile ahead‑of‑time:
gu install native-image
native-image -jar HelloWorld.jar
Produces a standalone binary (./helloworld) with no JRE required Not complicated — just consistent..
Best Practices Checklist
- ☐ Use a build tool (Maven/Gradle) for anything beyond a single file—handles dependencies, testing, and packaging automatically.
- ☐ Keep source and output separate (
src/vs.out/ortarget/). - ☐ Version‑control
.gitignorecompiled artifacts (*.class,*.jar,out/,target/). - ☐ Test on the target JRE (especially LTS versions: 8, 11, 17, 21).
- ☐ Document the run command in a
README.mdso collaborators don’t guess the classpath.
Conclusion
You now have a complete toolkit for taking a Java program from a single .Practically speaking, mastering the compile‑run-debug cycle at the terminal builds a mental model that makes IDE features (and their occasional quirks) far less mysterious. Here's the thing — with these fundamentals solidified, you’re ready to explore libraries, frameworks, and the vast Java ecosystem with confidence. Plus, java file to a runnable application—whether you prefer the transparency of the command line, the ergonomics of an IDE, or the portability of an executable JAR. Day to day, as your projects grow, adopt a build system early; it turns the manual steps demonstrated here into reproducible, automated pipelines. Happy coding!
The official docs gloss over this. That's a mistake.