[Setup] Java Development Environment
This page explains how to set up a Java development environment. Java requires compilation, but installing the JDK (Java Development Kit) is all you need to start developing.
Installing the JDK
Java development requires the JDK (Java Development Kit). The JDK includes both the compiler (javac) and the runtime environment (java).
| OS | Installation Method |
|---|---|
| Windows | Download and run the installer from Adoptium (formerly AdoptOpenJDK). Make sure to enable the option to add the environment variable PATH during installation. |
| macOS | Run brew install openjdk with Homebrew, or download the installer from Adoptium. |
After installation, run the following in a terminal (Command Prompt on Windows) to verify.
javac --version javac 21.0.3 java --version openjdk 21.0.3
If both commands display a version number, the installation was successful.
Compiling and Running
Java is a compiled language. The process involves two steps: compiling the source code (.java) to bytecode (.class), and then running it.
| Step | Command | Description |
|---|---|---|
| 1 | javac FileName.java | Compiles the source code and generates a .class file. |
| 2 | java ClassName | Runs the .class file (do not include the extension). |
Sample: Hello.java
Save the following content as Hello.java using a text editor. The file name must match the class name. In this example the class is named Hello, so the file must be named Hello.java.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
String name = "Alice";
System.out.println("Hello, " + name + "!");
for (int i = 1; i <= 5; i++) {
System.out.println("Loop iteration " + i);
}
}
}
Navigate to the folder where you saved the file in a terminal, then run the following commands in order.
Compile:
javac Hello.java
Run:
java Hello Hello, Java! Hello, Alice! Loop iteration 1 Loop iteration 2 ...
After a successful compilation, a Hello.class file is generated in the same folder.
Handling Compile Errors
If there is a mistake in the code, an error message is displayed when running javac. Here are some common errors.
| Error | Cause |
|---|---|
| class Hello is public, should be declared in a file named Hello.java | The file name and class name do not match. |
| reached end of file while parsing | Curly braces ({ and }) are not properly matched. |
| cannot find symbol | There is a typo in a variable name or method name. |
| '; ' expected | A semicolon (;) is missing at the end of a statement. |
Error messages include a line number. Check and fix the indicated line.
Command Not Found
If the terminal shows javac: command not found or java: command not found, the PATH may not be configured correctly. Follow the steps below to check and set it up.
1. Locate the command
Check where the javac command is installed.
which javac
If it is not found, check the common installation paths.
ls /usr/local/bin/javac ls /opt/homebrew/bin/javac
2. Check your shell
echo $SHELL
If the output is /bin/zsh, edit ~/.zshrc. If it is /bin/bash, edit ~/.bashrc.
3. Set JAVA_HOME and PATH
For Java, you need to set the JAVA_HOME environment variable in addition to PATH. Check the JDK installation path and configure both.
Check the JDK installation path on macOS.
/usr/libexec/java_home
For macOS (zsh) — set JAVA_HOME and PATH:
echo 'export JAVA_HOME=$(/usr/libexec/java_home)' >> ~/.zshrc echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.zshrc source ~/.zshrc
For Linux (bash):
echo 'export JAVA_HOME=/usr/lib/jvm/java-21-openjdk' >> ~/.bashrc echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.bashrc source ~/.bashrc
On Windows, configure the following under "System Properties" → "Environment Variables".
| Variable Name | Example Value |
|---|---|
| JAVA_HOME | C:\Program Files\Eclipse Adoptium\jdk-21 |
| Path (add) | %JAVA_HOME%\bin |
If you find any errors or copyright issues, please contact us.