Back to Java
2025-12-258 min read

Setting Up the Environment Path for Linux, UNIX, Solaris, FreeBSD (Java)

Learn Setting Up the Environment Path for Linux, UNIX, Solaris, FreeBSD (Java) step by step with clear examples and exercises.

Why This Matters

Setting up the environment path for Java on various Unix-based operating systems is crucial for smooth development and execution of Java programs. By configuring your system's environment path, you can run Java programs from any directory without having to specify the full path to the Java executable. This saves time and increases productivity, especially when working with multiple versions of Java or third-party tools that require a specific version of Java.

The environment path is a list of directories that the shell searches when you type a command. By default, the shell looks for executables in specific system directories such as /usr/bin, /bin, etc., but not in the directory where Java is installed. To run Java programs from any directory, we need to add the directory containing the Java executable (usually bin) to our environment path.

Prerequisites

Before proceeding with this tutorial, ensure you have:

  1. A Unix-based operating system (Linux, UNIX, Solaris, FreeBSD) installed on your machine.
  2. Basic knowledge of the command line and file system navigation.
  3. Java Development Kit (JDK) downloaded for your specific operating system. You can obtain it from the Oracle website.
  4. Familiarity with text editors like nano, vim, or emacs.
  5. Understanding of symbolic links (symlinks) and how they function in Unix systems.
  6. Basic understanding of the shell environment and configuration files such as ~/.bashrc, ~/.bash_profile, or ~/.profile.

Core Concept

The environment path is a list of directories that the shell searches when you type a command. By default, the shell looks for executables in specific system directories such as /usr/bin, /bin, etc., but not in the directory where Java is installed. To run Java programs from any directory, we need to add the directory containing the Java executable (usually bin) to our environment path.

Setting the Environment Path

  1. Open a terminal window.
  2. Find the directory where your JDK was installed. For example, if you downloaded the JDK from Oracle and extracted it to /usr/local, the Java executable will be located in /usr/local/java/jdk-version/bin. Replace jdk-version with the version number of your JDK (e.g., jdk1.8.0_321).
cd /usr/local
ls -l java | grep jdk
  1. Add the Java executable directory to your environment path by editing your shell configuration file (usually ~/.bashrc, ~/.bash_profile, or ~/.profile). Open the file using a text editor:
nano ~/.bashrc
  1. Add the following line to the end of the file, replacing jdk-version/bin with the path you found in step 2:
export PATH=$PATH:/path/to/your/jdk-version/bin
  1. Save and close the file (in nano, press Ctrl+X, then confirm by pressing Y).
  1. To make the changes take effect immediately, run the following command:
source ~/.bashrc
  1. Verify that Java is now in your environment path by checking the value of the PATH variable and running a simple Java program:
echo $PATH
java -version

You should see output similar to the following:

/path/to/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/path/to/your/jdk-version/bin
java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.321-b13, mixed mode)

Using Symbolic Links for Multiple Versions of Java

To manage multiple versions of Java on your system, you can create symbolic links (symlinks) to the executables in each version's bin directory and add those symlinks to your environment path. This allows you to easily switch between different versions of Java as needed.

  1. Navigate to the directory where you want to create the symlinks, usually /usr/local/bin.
cd /usr/local/bin
  1. Create symbolic links for each version of Java you wish to use:
ln -s /path/to/your/jdk1.8.0_321/bin/java java8
ln -s /path/to/your/jdk1.7.0_80/bin/java java7

Replace /path/to/your/jdk-version with the actual path to your Java installation directories.

  1. Add the /usr/local/bin directory to your environment path:
export PATH=$PATH:/usr/local/bin
  1. Now, when you run the java command, it will use the version of Java pointed to by the symlink. To specify a different version, simply change the symlink's target.

Worked Example

Let's set up the environment path for Java on a Linux system with JDK 8 installed in /usr/local/java/jdk1.8.0_321 and JDK 7 installed in /usr/local/java/jdk1.7.0_80.

  1. Open a terminal window.
  2. Find the directories where your JDKs were installed:
cd /usr/local
ls -l java | grep jdk

Output:

lrwxrwxrwx 1 root root 23 Nov 17 2020 java -> java-8-openjdk-amd64
drwxr-xr-x 9 root root 4096 Nov 17 2020 java-8-openjdk-amd64/
lrwxrwxrwx 1 root root 23 Nov 17 2020 java-7-openjdk-amd64 -> java-7-openjdk-amd64-2.7.0_80
drwxr-xr-x 7 root root 4096 Nov 17 2020 java-7-openjdk-amd64-2.7.0_80/
  1. Add the Java executable directories to your environment path by editing ~/.bashrc:
nano ~/.bashrc
  1. Add the following lines to the end of the file:
export PATH=$PATH:/usr/local/java/jdk1.8.0_321/bin
export PATH=$PATH:/usr/local/java/jdk1.7.0_80/bin
  1. Save and close the file (in nano, press Ctrl+X, then confirm by pressing Y).
  1. To make the changes take effect immediately, run the following command:
source ~/.bashrc
  1. Verify that Java is now in your environment path and can be switched between versions:
echo $PATH
java -version
java7 -version

Common Mistakes

  1. Forgetting to add the Java executable directory to your environment path.
  2. Adding the wrong directory or incorrect version of Java to your environment path.
  3. Not saving and closing the shell configuration file after editing it.
  4. Not sourcing the shell configuration file after making changes to it.
  5. Running the java command without specifying a specific Java program, resulting in an error message.
  6. Creating symlinks with incorrect names or pointing them to the wrong directories.
  7. Failing to add the /usr/local/bin directory to your environment path when using symbolic links for multiple versions of Java.
  8. Not restarting the terminal emulator after making changes to the environment path.
  9. Forgetting to update the symlinks when upgrading or downgrading Java versions.
  10. Using a shell configuration file that is not compatible with your current shell (e.g., using ~/.bashrc for csh).

Subheadings under Common Mistakes:

  • Incorrect Paths and Versions
  • Unsaved Changes
  • Untimely Sourcing
  • Generic Java Command
  • Incorrect Symlinks
  • Missing /usr/local/bin in the Environment Path
  • Not Restarting Terminal Emulator
  • Outdated Symlinks
  • Incompatible Shell Configuration File

Practice Questions

  1. You have multiple versions of Java installed on your system. How can you ensure that the correct version is used when running Java programs?
  • Create symbolic links for each version's executables in /usr/local/bin and add this directory to your environment path.
  1. What happens if you forget to add the Java executable directory to your environment path?
  • You will need to specify the full path to the Java executable when running programs, which can be inconvenient and error-prone.
  1. How do you find out which version of Java is currently in your environment path?
  • Run java -version in a terminal window. The output will display the version number.
  1. You have a Java program named MyProgram.java in your home directory. How can you run it without specifying the full path to the Java executable?
  • If the Java executable is in your environment path, you can simply type java MyProgram in the terminal window.
  1. What should you do if adding the Java executable directory to your environment path does not work as expected?
  • Check that you have saved and closed your shell configuration file after making changes to it. Then, source the file to apply the changes: source ~/.bashrc. If the problem persists, try logging out and logging back in, or restarting your terminal emulator. If you're still experiencing issues, double-check that the path to the Java executable directory is correct and that the JDK is properly installed on your system.

FAQ

Why can't I run Java programs from my current directory?

Java programs must be compiled and executed from a directory containing a valid class file, which is typically generated by compiling a .java source file using the javac command. If you try to run a Java program directly from your current directory (where there is no class file), you will receive an error message. To run the program, navigate to the directory containing the class file and execute it using the java command.

I've added the Java executable directory to my environment path, but I still can't run Java programs. What should I do?

First, make sure that you have saved and closed your shell configuration file after making changes to it. Then, source the file to apply the changes:

source ~/.bashrc

If the problem persists, try logging out and logging back in, or restarting your terminal emulator. If you're still experiencing issues, double-check that the path to the Java executable directory is correct and that the JDK is properly installed on your system.

How can I use a specific version of Java when multiple versions are installed?

To use a specific version of Java, you can create symbolic links (symlinks) for the executables in your preferred version's bin directory and add those symlinks to your environment path. For example:

ln -s /usr/local/java/jdk1.8.0_321/bin/java /usr/local/bin/java8
ln -s /usr/local/java/jdk1.7.0_80/bin/java /usr/local/bin/java7

Then, add the /usr/local/bin directory to your environment path:

export PATH=$PATH:/usr/local/bin

Now, when you run the java command, it will use the version of Java pointed to by the symlink. To specify a different version, simply change the symlink's target.

I have created symbolic links for multiple versions of Java but can only run one version. What should I do?

First, ensure that you have added the /usr/local/bin directory to your environment path:

export PATH=$PATH:/usr/local/
Setting Up the Environment Path for Linux, UNIX, Solaris, FreeBSD (Java) | Java | XQA Learn