Setting up Java development environment

In this lesson of Java course, we will be setting up Java development environment. Before we start developing application in Java or start our journey with our course, we need to download and install JDK. We have already gone through the different components of the Java development environment and this lesson is focused on setting up the Java development environment.

You can use our free online Java compiler to run your application / program without installing in on your local environment.

1. Setting up Java development environment

There are different options to download the JDK for your local development. When using in production, you might want to check for the license, however for the development, we have the following high level options

  • Open JDK.
  • JDK provided by Oracle

1.1. What is JDK

We talked about it in the last lesson, JDK stands for Java Development Kit. JDK contains a number of tools which helps us in Java application development. Here are some of the main components of the JDK.

  1. Java Compiler.
  2. Archiver (for application package).
  3. JRE to run Java program.

Download the JDK from Open JDK or Oracle site, make sure you download the correct installer based on your OS. Run the installer to install the JDK on your development environment.

2. Setting up the JAVA_HOME environment variable

One of the main step while setting up Java development environment is to set up the JAVA_HOME environment variable.Set up the JAVA_HOME environment variable to point to the directory where the JDK is installed. This step varies based on your operating system. Let’s see how to setup this on few OS.

2.1. Setting up JAVA_HOME environment variable on window

Here are the step by step guide to set up the JAVA_HOME environment variable on window machine.

  1. First, you need to locate the directory where you installed the Java Development Kit (JDK). Run where java on Command Prompt to check the JDK path.
  2. Right-click on “This PC” or “My Computer” (depending on your Windows version) and select “Properties.The default installation path is often something like C:\Program Files\Java\jdkX (where X represents the version number).
  3. Click on “Advanced system settings” on the left side.
  4. In the System Properties window, click the “Environment Variables” button at the bottom.
  5. Under “System variables,” click “New.”
  6. For “Variable name,” enter JAVA_HOME.
  7. For “Variable value,” enter the path to your JDK installation directory (e.g., C:\Program Files\Java\jdkX).
  8. Click “OK” to close all the windows.

2.2. Setting up JAVA_HOME environment variable on macOS and Linux

  1. Open a terminal window.
  2. Run the command: echo $(/usr/libexec/java_home). This will display the path to the currently active JDK installation.
  3. In the terminal, run the command: export JAVA_HOME=/path/to/your/JDK.Replace /path/to/your/JDK with the actual path to your JDK installation

To set the JAVA_HOME variables permanently, we can follow these steps:

  1. Open the .bashrc, .bash_profile, or .zshrc file in your home directory using a text editor.
  2. Add the following line at the end of the file: export JAVA_HOME=/path/to/your/JDK. Save the file and close the text editor.

To confirm the changes, run the following command in the terminal window

 ~ java -version

You will have some similar output:

Setting up Java development environment

3. Install Integrated Development Environment (IDE):

The next step is to choose the IDE to write and manage your Java code. There are many popular options, here are the 2 top popular options:

  1. IntelliJ IDEA – Download the Community Edition (free) – Recommended.
  2. Eclipse – Free

Install any IDE of your choice. Once this is done, We need to configure the IDE. I will walk you through setting you your IntelliJ IDEA but you can follow any online article to setup JDK for Eclipse.

3.1. Project Structure

Launch IntelliJ IDEA and Choose “Configure” > “Project Defaults” > “Project Structure.”

Setting up Java development environment
3.2. Setup the JDK

Under “Project,” set the Project SDK to the JDK you installed

This will ensure that your IDE is all set for your first Java program. In the next lesson we will talk about creating our first Java program.

Summary

In this lesson, we followed the steps for setting up Java development environment. This is the first step to start our learning jounrey for Java application development. We will continue our learning journey from here. As always, the entire source code for this course is available on the GitHub repository.