In this tutorial, we shall make Java environment setup for Windows, Ubuntu, and macOS so that you can compile and run Java programs from the command line.

Install Java for Programming on Windows, Ubuntu, and macOS

To start Java programming, install the Java Development Kit, set the Java path if your operating system does not configure it automatically, and verify the installation using java -version and javac -version.

Following are the steps to get your computer ready for Java programming:

  1. Install JDK.
  2. Setup Environment Path.
  3. Verify the Java installation.

Should You Install JDK or JRE for Java Development?

If you want to write, compile, and run Java programs, install the JDK. The JDK includes development tools such as javac, which is the Java compiler. The JRE is mainly for running Java applications and is not enough for compiling Java source files.

For most learners and developers, the correct choice is the latest stable JDK for your operating system. If you only need Java to run an existing desktop application, the JRE or a runtime bundled with that application may be enough.

1. Install Java Development Kit JDK

JDK contains everything that is required for writing a Java program, compiling it to byte code, and running it on your machine. JDK is comprised of following components:

  • Java Language – which specifies the syntax and core components.
  • Tools and Tool APIs – these are the tools and packages used while developing Java applications.
  • JRE – The environment for running the Java byte code.

2. Download Latest Java JDK Installer

Based on your operating system, Linux/Mac OS X/Solaris/Windows, and 32bit/64bit, download a jdk package from the below url
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
And follow the instructions of the installer.

The above link points to an older JDK 8 download page. For a new setup, you may also use the current official Java download pages depending on your need:

Choose the installer that matches your operating system and processor architecture. For modern Windows and macOS computers, this is usually a 64-bit installer.

3. Setup Java Path and JAVA_HOME

For the operating system to know about the location of Java commands such as java and javac, environment variables are to be setup. Many installers can add Java to the path automatically, but it is useful to know how to check and configure it manually.

Setup Java Path in Windows

Following is a step by step guide to setup Java Path in Windows computer.

  1. Install the JDK using the Windows installer.
  2. Find the JDK installation folder. It is commonly similar to C:\Program Files\Java\jdk-21\bin or another version-specific folder under C:\Program Files\Java.
  3. Go to Advanced Environment Variables Window, Click on Windows button and navigate through the path
    Control Panel -> System and Security -> System -> Advanced system settings -> Advanced -> Enviroment variables -> Advanced
  4. Edit “Path” environment variable, create new variable with name “Path” if already not existing
  5. Append the path of the java executable (path of java.exe in the Java installed folder) to the “Path” environment variable. For example, if path is C:\WINDOWS\SYSTEM32 , make it C:\WINDOWS\SYSTEM32;C:\Program Files\Java\jdk1.7.0_40\bin  assuming the path of your java installation to be <span class="lang:default decode:true crayon-inline ">C:\Program Files\Java\jdk1.7.0_40\bin</span>.
  6. Open a new Command Prompt window and verify Java using java -version and javac -version.

You can also check Java from Command Prompt with the following commands.

</>
Copy
java -version
javac -version

Setup Java Path in Ubuntu

Following is a step by step guide to setup Java Path in Ubuntu computer.

On Ubuntu, you can install a JDK from the package manager. The exact package name can vary by Ubuntu release, but OpenJDK packages are commonly available from the default repositories.

</>
Copy
sudo apt update
sudo apt install default-jdk

After installation, check the installed Java commands.

</>
Copy
java -version
javac -version

Step 1: Open a Terminal and run the following command to edit environment variables

$ sudo nano /etc/environment

Provide the password if prompted for.

Step 2: Add JAVA_HOME to the file. Append the following line to the end of the file /etc/environment.

JAVA_HOME=”/usr/lib/jvm/open-jdk”

Replace the value with your Java Installation Path.

Step 3: Save the file and exit.

To find the installed Java location on Ubuntu, the following command is often useful.

</>
Copy
readlink -f $(which java)

Setup Java Path in macOS

On macOS, install a JDK using the official installer for macOS. After installation, open Terminal and check Java.

</>
Copy
java -version
javac -version

If you need to set JAVA_HOME manually on macOS, you can use the /usr/libexec/java_home helper.

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

To make this change permanent for the current user, add those lines to the appropriate shell profile file, such as ~/.zshrc for the default zsh shell.

Install Java from Command Line

Command-line installation is common on Linux and is also available on some systems through package managers. On Ubuntu, use apt. On macOS, developers often use a package manager or the official installer. On Windows, the graphical JDK installer is the simplest option for beginners.

Ubuntu command-line example:

</>
Copy
sudo apt update
sudo apt install default-jdk
java -version
javac -version

The important point is to verify both Java runtime and Java compiler commands after installation.

4. Verify the Installation of Java

Let us check if Java has been installed correctly. Open command prompt in Windows or terminal in Ubuntu and run the following command

$ java -version

If Java is visible in the environment, javaw.exe responds to the command java -version with the result shown in the following picture.

Java Environment Setup
Java Version

The result to java -version, from the above picture notifies us the following:

  1. “1.8.0_111” is the version of JDK installed in the computer (running on OS : ubuntu 16.04)
  2. “1.8.0_111” is the version of JRE
  3. Java Virtual Machine is running in mixed mode. There are two basic modes namely Server mode and Client mode, in which Server mode is optimized for long running and Client mode is optimized for maximum performance. Running in mixed mode result in best of Client and Sever modes : long running and better performance.

For a development setup, also verify the compiler.

</>
Copy
javac -version

If java -version works but javac -version does not work, you may have installed only a runtime or your JDK bin directory may not be added to the path.

Compile and Run a Java Program After Installation

After Java is installed, create a simple program and test the complete compile-and-run flow. Create a file named HelloWorld.java.

</>
Copy
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

Compile and run the program from the folder where the file is saved.

</>
Copy
javac HelloWorld.java
java HelloWorld

Expected output:

Hello, Java!

Multiple JREs or JDKs on the Same Computer

Multiple JREs and JDKs could be installed alongside one another and any of the installed could be used for compiling or executing a java program.

When multiple Java versions are installed, the version used in a terminal depends on the order of directories in the Path variable or the active system alternatives configuration. Always run java -version and javac -version in a new terminal window after changing Java path settings.

Common Java Installation Problems and Fixes

java is not recognized on Windows

This usually means the JDK bin folder is not present in the Path environment variable, or the Command Prompt window was opened before the path was updated. Add the JDK bin path and open a new Command Prompt window.

javac command not found after Java installation

If java works but javac does not, check whether you installed a full JDK. A runtime-only installation may run Java applications but will not compile Java source code.

JAVA_HOME points to the wrong Java folder

JAVA_HOME should point to the JDK root folder, not the bin folder. The Path variable should include the bin folder. For example, JAVA_HOME may point to C:\Program Files\Java\jdk-21, while Path includes C:\Program Files\Java\jdk-21\bin.

FAQs on Installing Java

How can I install Java on my computer?

Download the JDK installer for your operating system, run the installer, set the Java path if required, and verify with java -version and javac -version. For Ubuntu, you can also install Java from the command line using sudo apt install default-jdk.

Do I install JDK or JRE for Java programming?

Install the JDK for Java programming. The JDK includes the compiler and development tools. The JRE is mainly used to run Java applications and is not enough when you need to compile .java files.

Can I download Java for free?

Java development kits are available from official vendors and OpenJDK distributions. Always check the license terms for the specific JDK distribution and version you download, especially for commercial or production use.

How do I install Java from command line on Ubuntu?

Run sudo apt update, then run sudo apt install default-jdk. After installation, verify it with java -version and javac -version.

Install Java Editorial QA Checklist

  • Confirm that the tutorial tells beginners to install the JDK, not only the JRE, for Java programming.
  • Check that Windows, Ubuntu, and macOS setup paths are covered separately.
  • Verify that command-line examples use java -version and javac -version.
  • Ensure that new code blocks use PrismJS-compatible classes such as language-bash, language-java, and output.
  • Keep the existing Java version image and original links unchanged while adding current official download references where useful.

Install Java Next Steps

In this Java Tutorial – Install Java, we have successfully installed Java and setup Java Path in the environment variables. Now the computer’s environment is ready to handle java development. In our next Java Tutorial we shall see some of the Integrated Development Environments (IDE) for Java that make Java Programming Development easy, followed by our first program in Java – HelloWorld.java.