How you can Set JAVA_HOME in Ubuntu Linux

How you can Set JAVA_HOME in Ubuntu Linux

This tutorial is about the How you can Set JAVA_HOME in Ubuntu Linux. We will try our best so that you understand this guide. I hope you like this blog How you can Set JAVA_HOME in Ubuntu Linux. If your answer is yes then please do share after reading this.


Check How you can Set JAVA_HOME in Ubuntu Linux

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

In this beginner tutorial, I will show the steps to properly set the Java startup variable in Ubuntu. The steps should also apply to most other Linux distributions.

The process consists of these steps:

  • Make sure the Java Development Kit (JDK) is installed.
  • Find the correct location of the JDK executable.
  • Set the JAVA_HOME variable and make the change permanent.

Step 1: check if JDK is installed

The easiest way to check if the Java Development Kit (JDK) is installed on your Linux system is to run this command:

javac –version

The above command checks the Java compiler version. If installed, it will show the Java version.

check ubuntu java compiler

Java compiler is installed

If the command shows an error like javac command not found, you will have to install JDK.

Java compiler check on ubuntu

Java compiler is not installed

If the Java compiler is not installed on your system, install the Java development kit with this command:

sudo apt install default-jdk

This will install the default version of Java on your current version of Ubuntu. If you need any other specific version of Java, you will need to specify it when installing Java on Ubuntu.

Once you have made sure 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 of the Java executable.

Use the which command to get the location of the Java compiler executable:

what javac

The problem here is that the location you give is actually a symbolic link. You will have to follow it a couple of times:

get java ubuntu boot path

And when you find a path like / usr / lib / jvm / java-11-openjdk-amd64 / bin / javac, remove the / bin / javac to get something like / usr / lib / jvm / java-11- openjdk-amd64

An easier method is to follow the symbolic link and get to the actual executable file directly using this command:

readlink -f `which javac` | sed “s: / bin / javac ::”

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

In my example the executable file location is / usr / lib / jvm / java-11-openjdk-amd64. It could be different for you. Copy the correct path you got from the above command on your system. You know, you can copy paste in Ubuntu terminal.

Step 3: Setting the JAVA_HOME variable

Now that you have the location, use it to set the JAVA_HOME environment variable:

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

Check the value of the JAVA_HOME directory:

echo $ JAVA_HOME

set java home ubuntu linux

set java home ubuntu linux

Try running your program or project on the SAME TERMINAL and see if it works.

This is not over yet. 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 need to add it to the bashrc file in your home directory.

You can use the Nano editor to edit files in the Linux terminal. If you don’t want that and take a simple copy and paste approach, use the following commands:

Make a backup of your bashrc file (in case you mess it up, you can get it back):

cp ~ / .bashrc ~ / .bashrc.bak

Then use the echo command to add the export command you used at the beginning of this section. Change the command below to use the correct path as shown by your system in.

echo “export JAVA_HOME = / usr / lib / jvm / java-11-openjdk-amd64” >> ~ / .bashrc

Verify that it was correctly added to the end of the file:

queue -3 ~ / .bashrc

The above queue command will display the last 3 lines of the specified file.

Here is the full output of the three commands above.

setting java home bashrc

Now, even if you log out or reboot the system, the JAVA_HOME variable will still be set to the value you specified. That’s what you want, right?

From the news itsfoss.com

Final words: How you can Set JAVA_HOME in Ubuntu Linux

I hope you understand this article How you can Set JAVA_HOME in Ubuntu Linux, if your answer is no then you can ask anything via contact forum section related to this article. And if your answer is yes then please share this article with your family and friends.

Bollyinside - Get Daily Blogs From Experts
Logo