How to Set JAVA_HOME in Ubuntu Linux – Guide

If you are running Java programs on Ubuntu using Eclipse, Maven or Netbeans etc, you will need to set JAVA_HOME to your path. Otherwise, your system will complain that “the environment variable java_home is not set”.

In this beginner tutorial, I’ll show you how to set up Java Home variable in Ubuntu. The steps should also be valid for most other Linux distributions.

The process consists of the following steps:

The Java Development Kit (JDK) is a set of tools that allow you to develop Java applications.

To find the correct location of the Java Development Kit (JDK) executable, open a command prompt and type the following: java -version ..

Setting the Java environment variable and making the change permanent.

Step 1: Make sure the JDK is installed

java -version If the JDK is installed, you will see output that looks like this: java version “1.8.0_131” Java(TM) SE Runtime Environment (build 1.8.0_131-b13) ..

The Java compiler is installed and it will show the Java version.

Ubuntu Java compiler is a tool that helps you compile and run Java applications.

Java is installed on the computer.

If you get an error like “javac command not found,” you’ll need to install the JDK. ..

A Java compiler check on Ubuntu was successful.

A Java compiler is not installed on your computer. ..

java -version

This will install the default Java Development Kit. ..

This will install the default Java version on your current Ubuntu version. If you need a different version of Java, you will have to specify it when installing Java on Ubuntu. ..

After verifying that the Java Compiler is present on your system, it is time to find its location.

Step 2: Get the location of the JDK executable (Java Compiler)

The executable is usually located in the /usr/lib/jvm directory. I won’t leave you alone for a guessing game. Instead, let’s find out the path to the Java executable.

which javac

The problem is that the link provided leads to a symbolic link. You will have to follow it a few times to get to the content. ..

Get Java Home on Ubuntu:

  1. First, determine the Java Home on your Ubuntu system. This can be found by running the following command:
  2. Once you have determined the Java Home, you can use the following command to get it:
  3. Finally, you can use the getjavahome command to get the Java Home on Ubuntu:

When you find a path like /usr/lib/jvm/java-11-openjdk-amd64/bin/javac, you remove /usr/bin/javac from it to get something like /usr/lib/jvm/java-11- openjdk -amd64.

c:\windows\system32\cmd.exe /c “netstat -an” This command will show you the active network connections and their status.

The command “readlink -f which javac” will print out the path to the Java compiler. To disable the Java compiler from being executed, use the command “sed ’s: / bin / javac ::’”. ..

The readlink command follows a symbolic link. I used `around which java. This is called command substitution and replaces the command with its output. Sed is then used to replace /bin/javac with nothing and thus remove it altogether.

In my example, the executable file is located at /usr/lib/jvm/java-11-openjdk-amd64/. Copy the correct path obtained with the above command to your system.

Step 3: Set the JAVA_HOME variable

  1. Open a terminal window and type the following command: export JAVA_HOME=/usr/lib/jvm/java-8-oracle
  2. Restart your computer to apply the new environment variable. ..

export JAVA_HOME = / usr / lib / jvm / java-11-openjdk-amd64

To find out the value of the JAVA_HOME directory, use the following command: java -version ..

Java is a programming language that runs on the Java Platform, which is a platform provided by Ubuntu Linux. ..

The Java home on Ubuntu Linux is /usr/lib/jvm/java-1.8.0-openjdk/.

If you’re trying to run your program or project on the same terminal as someone else, see if it works.

This is not the end. The JAVA_HOME variable you just declared is temporary. If you close the terminal or start a new session, it will be empty again. ..

To set the JAVA_HOME variable ‘permanently’, you must add it to the ~/.bashrc file in your home directory.

nano -w /etc/passwd nano -w /etc/shadow nano -w /etc/group

bashrc

source ~/.bashrc

export BASH_ENV= " $BASH_ENV "

cp -R ~ / .bashrc ~ / .bashrc.bak

echo “export CMD=/usr/bin/env CMD” export CMD=/usr/bin/env CMD

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

To ensure that your changes were correctly added to the final version of the file, you should check that the changes were made in the correct location. ..

This command will run the ~/.bashrc file in the current directory, and will append the contents of that file to the end of the current working directory.

The tail command will show the last 3 lines of the current file.

The three commands above produce the following output.

export JAVA_HOME=/usr/local/java export PATH=/usr/local/java:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/opt/java

If you want to change the JAVA_HOME environment variable, you can do so by logging out and then back in or restarting the system. ..

Final note

In this guide, we will show you how to set Java home in Ubuntu Linux. If you have any questions or concerns about this article, please feel free to ask us in the comments section below. Additionally, please share this guide with your friends so they can learn how to set Java home as well. ..