Java Installation & Running Your First Program

A complete step-by-step guide to install Java Development Kit (JDK) and create your first Java program using Sublime Text or Notepad.

1. What You Need To Know

Before writing Java code, you need to install the Java Development Kit (JDK) on your computer. The JDK includes the compiler (javac) and Java Runtime Environment (JRE) needed to compile and run Java programs. Once installed, you can write Java code in any text editor like Sublime Text or Notepad, compile it using the command line, and execute it.

Important: You need JDK (not just JRE) to compile Java programs. JRE alone can only run pre-compiled programs.

  • JDK: Java Development Kit - used by developers to write and compile Java code
  • JRE: Java Runtime Environment - used to run compiled Java programs
  • Compiler: Converts your Java code (.java files) into bytecode (.class files)

2. Download & Install the Java Development Kit (JDK)

The first step is to download and install the JDK on your computer. Follow the steps below based on your operating system.

Step 1: Download JDK

Visit the official Oracle Java website and download the latest version of JDK for your operating system (Windows, macOS, or Linux). You can also use OpenJDK, which is free and open-source.

Official Download Link: https://www.oracle.com/java/technologies/downloads/

Step 2: Run the Installer

Once downloaded, run the installer file (.exe for Windows, .dmg for macOS, or .tar.gz for Linux). Follow the installation wizard and click "Next" through all prompts. The default installation path is usually fine.

Default Installation Path (Windows): C:\Program Files\Java\jdk-version

Step 3: Verify Installation

Open Command Prompt (Windows) or Terminal (macOS/Linux) and type the following commands to verify that Java was installed correctly:

Verify Java Installation


java -version
javac -version
Expected Output:
java version "21.0.1" 2023-10-17
Java(TM) SE Runtime Environment (build 21.0.1+12-39)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-39, mixed mode, sharing)

Tip: If you see "command not found" or "is not recognized", Java may not be installed correctly or the environment variables are not set up. Proceed to Section 3.

3. Set Up Environment Variables

Environment variables tell your operating system where Java is installed. This allows you to run Java commands from any directory in your command prompt or terminal.

For Windows:

Step 1: Right-click on "This PC" or "My Computer" and select "Properties"

Step 2: Click on "Advanced system settings" and then "Environment Variables"

Step 3: Under "System variables", click "New" and create a variable:

  • Variable name: JAVA_HOME
  • Variable value: C:\Program Files\Java\jdk-21 (adjust version number if needed)

Step 4: Find the "Path" variable in the list and click "Edit". Add this to the end: %JAVA_HOME%\bin

Step 5: Click OK to save. Restart your Command Prompt.

For macOS/Linux:

Open Terminal and add the following lines to your shell profile file (~/.bash_profile, ~/.zshrc, or ~/.bashrc):

Set JAVA_HOME in macOS/Linux


export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH

After making changes, run this command to apply them:

Apply Changes


source ~/.zshrc

4. Set Up Your Text Editor

You can write Java code in any text editor. Here's how to set up Sublime Text or use Notepad.

Option 1: Using Sublime Text

Step 1: Download and install Sublime Text from https://www.sublimetext.com/

Step 2: Open Sublime Text and create a new file (File → New File)

Step 3: Write your Java code (we'll do this in the next section)

Step 4: Save the file with a .java extension: File → Save As → HelloWorld.java (Note: the filename must match the public class name)

Option 2: Using Notepad

Step 1: Open Notepad (Windows) or TextEdit (macOS)

Step 2: Write your Java code

Step 3: Save the file with a .java extension: File → Save As → select "All Files" → HelloWorld.java

Important: Make sure the file extension is .java, not .txt

Note: Java is case-sensitive. The filename must exactly match the public class name defined in the code (e.g., if your class is named HelloWorld, the file must be HelloWorld.java).

5. Write Your First Java Program

Now let's write a simple Java program that prints "Hello, World!" to the screen. This is the traditional first program that every programmer writes.

Example: Hello World Program


public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Output:
Hello, World!

Explanation:

  • public class HelloWorld: Defines a public class named HelloWorld. The filename must be HelloWorld.java
  • public static void main(String[] args): The main method is the entry point of any Java program. The program execution starts here
  • System.out.println(): Prints text to the console and moves to the next line
  • "Hello, World!": The string that will be printed to the screen

Important: The class name must be public and must match the filename exactly (including capitalization). Always include the main method in your class for a program to run.

6. Compile & Run Your Program

Once you've written your Java code and saved it, you need to compile it using the javac command and then run it using the java command.

Step 1: Open Command Prompt/Terminal

Open Command Prompt (Windows) or Terminal (macOS/Linux) and navigate to the directory where you saved your Java file.

Navigate to Your Java File Directory


cd C:\Users\YourUsername\Desktop

Step 2: Compile Your Java File

Use the javac command to compile your Java file. This creates a .class file (bytecode) from your .java file.

Compile the Java File


javac HelloWorld.java
Expected Result:
No output means compilation was successful. A HelloWorld.class file is created.

Step 3: Run Your Compiled Program

Use the java command to run your compiled program. Note: use the class name without .class extension.

Run the Java Program


java HelloWorld
Output:
Hello, World!

Tips: If you get an error "class not found" or "cannot find symbol", check that: (1) the filename matches the class name exactly, (2) you saved the file with .java extension, (3) you're in the correct directory.

7. Common Errors & Troubleshooting

Here are common errors you might encounter and how to fix them:

Error 1: "javac is not recognized as an internal or external command"

Cause: Java is not installed or environment variables are not set up correctly.

Solution: Reinstall JDK and properly set up the JAVA_HOME environment variable and add it to Path (see Section 3).

Error 2: "Exception in thread 'main' java.lang.NoClassDefFoundError"

Cause: The class file is not in the current directory or the class name is typed incorrectly.

Solution: Make sure you're in the correct directory and use the exact class name. Type "dir" (Windows) or "ls" (macOS/Linux) to see files in your current directory.

Error 3: "Error: Could not find or load main class HelloWorld"

Cause: The compiled .class file doesn't exist in the current directory or the filename doesn't match the class name.

Solution: Recompile the Java file using javac HelloWorld.java, then verify that HelloWorld.class exists in your directory.

Error 4: "Syntax Error" during compilation

Cause: Your code has syntax errors (missing semicolons, wrong brackets, typos, etc.).

Solution: Check the error message, find the line number mentioned, and correct your code. Common issues: missing semicolons at end of lines, mismatched brackets, incorrect spelling of keywords.

Debugging Tip: When you get an error, the compiler usually tells you the line number where the error is. Go to that line and carefully review your syntax.