You Have the Code, Now What?
You’ve just written your first few lines of Java, maybe a simple “Hello, World!” program. The code sits in your editor, looking perfect. But when you try to run it, nothing happens, or you’re met with a confusing error message in the terminal. This moment of friction is where many new programmers get stuck. The gap between writing code and actually starting a Java program can feel surprisingly wide.
Starting a Java program isn’t just about clicking a “run” button, though modern tools make it seem that way. It’s a fundamental process that involves compiling your human-readable source code into machine-understandable bytecode and then executing it within the Java Virtual Machine. Understanding this process from the ground up is the key to unlocking true confidence as a Java developer.
This guide will walk you through every method, from the bare-metal command line to sophisticated Integrated Development Environments. By the end, you’ll know exactly how to start a Java program in any context, troubleshoot common launch failures, and understand what’s happening under the hood.
The Absolute Prerequisites
Before you can start any Java program, you need the right tools installed on your computer. Think of this as setting up your workshop before building a piece of furniture.
Installing the Java Development Kit
The single most important component is the Java Development Kit, or JDK. It’s a common mistake to install only the Java Runtime Environment, which lets you run programs, but not create them. The JDK includes the compiler and other essential tools.
To check if you have a JDK installed and see its version, open your terminal or command prompt and type:
javac -version
If you see a version number like “javac 17.0.10” or similar, you’re good to go. If you get a “command not found” error, you need to install a JDK.
Head to the official Oracle website or adoptium.net to download the installer for your operating system. For most beginners, the latest Long-Term Support version is the best choice. Run the installer, and on Windows or macOS, the PATH environment variable is usually set for you. On Linux, you may need to update your alternatives or profile scripts.
Choosing Your Code Editor
You can write Java in anything from a simple text editor to a full-featured IDE. Your choice dictates the complexity of the “start” process.
– A plain text editor like Notepad++ or VS Code requires you to use the command line to compile and run.
– An Integrated Development Environment like IntelliJ IDEA, Eclipse, or NetBeans handles compilation and execution with a single click, but abstracts the underlying steps.
For learning the fundamentals, starting with the command line is invaluable. It demystifies the process. Once you understand that, moving to an IDE will make you more productive without losing sight of how things work.
Starting from the Command Line: The Core Method
This is the universal method that works everywhere. It involves two distinct commands: one to compile, and one to run.
Step 1: Write and Save Your Source File
Create a new file named HelloWorld.java. The filename must exactly match the public class name inside it, including case sensitivity. Write the classic program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
Save this file in a dedicated folder, like C:\java_projects or ~/java_projects. Navigate to this folder in your terminal using the cd command.
Step 2: Compile with javac
The javac command is the Java compiler. It translates your .java source file into a .class file containing bytecode. In your terminal, type:
javac HelloWorld.java
If the compilation is successful, you won’t see any output. A new file named HelloWorld.class will appear in the same directory. This is your compiled program. If you see errors, they are usually syntax mistakes—a missing semicolon, a typo in System.out.println, or a mismatched filename and class name.
Step 3: Execute with java
This is the moment of truth. To start the program, you use the java command, which launches the Java Virtual Machine and tells it to execute the bytecode in your .class file. The critical detail is that you do not include the .class extension. Type:
java HelloWorld
You should see “Hello, World!” printed in your terminal. Congratulations, you’ve just started a Java program from scratch. The JVM located the HelloWorld class, loaded its bytecode, found the main method—the universal entry point for all Java applications—and began execution.
Organizing Projects with Packages
Real-world programs aren’t single files in a root folder. They use packages to organize code. Starting a program in a package requires a slight adjustment.
Imagine your HelloWorld.java file now begins with package com.myapp;. You must place it in a directory structure that mirrors the package name: com/myapp/. From the root directory above com, you compile it with:
javac com/myapp/HelloWorld.java
To run it, you must be in the root directory and use the fully qualified class name:
java com.myapp.HelloWorld
The JVM uses the classpath—a list of directories and JAR files to search—to locate your class. By default, the current directory is on the classpath. When you specify com.myapp.HelloWorld, the JVM looks for a file at ./com/myapp/HelloWorld.class.
Using an Integrated Development Environment
IDEs automate the compile-and-run cycle into a single action, dramatically speeding up development. The process is visually guided but follows the same principles.
Setting Up a New Project in IntelliJ IDEA
After launching IntelliJ, select “New Project.” Choose “Java” as the project type, ensure the correct JDK is selected in the dropdown, and don’t select any additional libraries or frameworks for a simple start. Click “Create.”
The IDE creates a project structure with a src directory. Right-click on src, select New -> Java Class, and name it HelloWorld. IntelliJ will automatically create the class with a basic skeleton. Add your main method and print statement.
To start the program, you have several options: click the green play arrow in the gutter next to the main method, right-click the file in the project pane and select “Run ‘HelloWorld.main()'”, or use the keyboard shortcut (Ctrl+Shift+F10 on Windows/Linux, Shift+Control+R on macOS). The IDE handles compilation in the background and opens a “Run” tool window at the bottom to show your program’s output.
Understanding the Eclipse Workflow
In Eclipse, start by creating a new Java Project via File -> New -> Java Project. Give it a name and click Finish. Inside the project in the Package Explorer, right-click the src folder and select New -> Class. Name it HelloWorld and check the box that says “public static void main(String[] args)”. Click Finish.
Eclipse will generate the class with the main method stub. Add your print statement. To run, click the green “Run” button on the toolbar, select Run -> Run from the menu, or right-click the file and select Run As -> Java Application. The console view will display the output.
The major advantage of an IDE is instant feedback. Syntax errors are underlined in red as you type, and the run configuration manages classpaths and arguments for you.
Common Hurdles and How to Solve Them
Even with clear steps, things can go wrong. Here are the most frequent issues when trying to start a Java program.
“Error: Could not find or load main class”
This is the most common error message. It means the java command couldn’t locate your .class file. The causes are almost always related to the classpath or your current directory.
– You are in the wrong directory. If your class is in ./com/myapp/, you must run java com.myapp.HelloWorld from the directory that contains the com folder, not from inside the myapp folder.
– You included the .class extension. The command is java HelloWorld, not java HelloWorld.class.
– The class was not compiled. Ensure you ran javac successfully and that the .class file exists.
– A typo in the class name. Java is case-sensitive. HelloWorld is not the same as helloworld.
“javac is not recognized as an internal or external command”
This Windows error means the JDK’s bin directory is not in your system’s PATH environment variable. The installer may have missed it, or you might have multiple Java versions causing conflict.
To fix this, you need to find where your JDK is installed (e.g., C:\Program Files\Java\jdk-21\bin) and add that full path to the PATH variable via System Properties -> Environment Variables. After updating, close and reopen your command prompt for the change to take effect.
Dealing with Multiple Java Versions
It’s possible to have an older JRE for running software and a newer JDK for development. This can lead to confusion where javac points to version 21 but java points to version 8.
Use javac -version and java -version to check. If they differ, you need to adjust your PATH so the JDK’s bin directory comes before any other Java paths. On Linux and macOS, tools like update-alternatives can help manage this.
Beyond the Basics: Running Packaged Programs
When you share or deploy a Java application, you typically bundle all your .class files into a single Java Archive file, or JAR. Starting a program from a JAR is a essential skill.
First, you need to create an executable JAR. This requires a manifest file that specifies the main class. You can create one using the jar command or, more easily, have your IDE build it for you. In IntelliJ, go to File -> Project Structure -> Artifacts, add a JAR artifact, and specify the main class. Then build it via Build -> Build Artifacts.
To run a JAR file from the command line, use the -jar flag:
java -jar MyApplication.jar
The JVM reads the manifest inside the JAR to find the main class and begins execution. All the necessary classes are contained within the archive.
Your Launchpad to Java Development
Starting a Java program is the fundamental loop of development: write, compile, run, observe, repeat. Mastering this loop, whether through the transparent control of the terminal or the streamlined efficiency of an IDE, turns abstract code into working software.
The next step is to build upon this foundation. Create a program that takes user input using the Scanner class. Then, write a program with multiple classes in different packages and practice compiling and running them from the command line. Finally, package a small project into an executable JAR file and run it on a different computer to simulate deployment.
Each time you press run, you’re not just executing code—you’re testing an idea. With this complete guide, that process is now firmly in your control. Go ahead and start something.