Setting up a Java development environment is straightforward. Follow these steps to start coding in minutes!
Table of Contents
Step 1: Install Java Development Kit (JDK)
For All Operating Systems
1. Download JDK
The JDK (Java Development Kit) is a software development kit that contains the tools you’ll need to create, run, and debug Java programs. You can download the latest version of the JDK from following links. Make sure to select the appropriate version for your operating system (Windows, Mac, or Linux).
- Visit Oracle JDK or OpenJDK.
- Choose the latest LTS version (e.g., JDK 17).
2. Install JDK
- Windows: Run the downloaded
.exe
file and follow prompts. - macOS: Open the
.dmg
file and drag JDK to Applications. - Linux: Use package managers like
apt
oryum
.
sudo apt install openjdk-17-jdk # Debian/Ubuntu
sudo yum install java-17-openjdk # CentOS/RHEL
Step 2: Set Environment Variables (PATH)
Ensure your system recognizes javac
and java
commands.
Windows:
- Search for “Edit environment variables”.
- Under the System Variables section, scroll down and find the Path variable.
- Click on Edit and then click the New button.
- Add the JDK
bin
folder path (e.g.,C:\Program Files\Java\jdk-17\bin
). - Click OK to save the changes.
Linux Ubuntu machine
To set the Java home path permanently in Ubuntu, you need to follow the steps given below:
1. Find Java Installation Location
First, you need to find the location of your Java installation by running the following command in the terminal:
sudo update-alternatives --config java
This will display a list of all installed Java versions along with their paths. In my system I got the following output:
There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-1.8.0-amazon-corretto/jre/bin/java
Nothing to configure.
You may see a similar kind of output.
2. Note down the path of the Java version you want to use as the default.
3. Next, open the /etc/environment
file using a text editor with root privileges, for example:
sudo vim /etc/environment
I prefer to use Vim. It’s your choice which editor to use. If you want to use the nano then you can use the command:
sudo nano /etc/environment
4. Define JAVA_HOME Path
In the environment
file, add the following line at the end (Important: don’t change existing values.)
JAVA_HOME="/path/to/java"
Replace /path/to/java
with the path you noted down in step 2.
In my case, the value is: /usr/lib/jvm/java-1.8.0-amazon-corretto/jre/bin/java
5. Save and close the file.
If you are using the vim editor like me, then to save and exit the editor press ESE key and type:
!wq
This will save the changes and exit the editor.
6. Apply Changes
To apply the changes, run the following command in the terminal:
source /etc/environment
This will reload the environment variables.
7. Check Java Home Path
Finally, you can verify that the Java home path has been set correctly by running the following command:
echo $JAVA_HOME
This should output the path you set in step 4.
Step 3: Verify Installation
Open a terminal/command prompt and run:
java -version
javac -version
After executing above command, you may see an output like this:
openjdk 11.0.25 2024-10-15 LTS
OpenJDK Runtime Environment Corretto-11.0.25.9.1 (build 11.0.25+9-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.25.9.1 (build 11.0.25+9-LTS, mixed mode)
If you can see this output then, you are able to setup Java in your computer.
Step 4: Choose an IDE (Integrated Development Environment)
Pick a code editor to write Java efficiently:
1. IntelliJ IDEA (Recommended)
- Download Community Edition.
- Create a new project and start coding!
2. VS Code (Lightweight)
- Install VS Code.
- Add the Java Extension Pack from the Extensions marketplace.
3. Eclipse
- Download Eclipse IDE.
- Choose “Eclipse IDE for Java Developers”.
Step 5: Write Your First Java Program
- Create a file
HelloWorld.java
:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- Compile and run:
javac HelloWorld.java
java HelloWorld
Output:
Hello, World!
Troubleshooting Common Issues
- “javac not recognized”: Recheck the PATH setup.
- IDE errors: Ensure the JDK path is configured in IDE settings.
- Version conflicts: Uninstall older JDK versions if needed.
Conclusion
You’re ready to code in Java! With the JDK installed and an IDE set up, dive into projects or tutorials.