How you can Set JAVA_HOME in Ubuntu Linux

How you can Set JAVA_HOME in Ubuntu Linux

This guide is about How you can Set JAVA_HOME in Ubuntu Linux. So read this free guide, How you can Set JAVA_HOME in Ubuntu Linux step by step. If you have query related to same article you may contact us.

How you can 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 the steps to correctly 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:

  • Make sure the Java Development Kit (JDK) is installed.
  • Find the correct location of the JDK executable.
  • Setting the JAVA_HOME variable and making the change permanent.
  • Step 1: Make sure the JDK is installed

    The simplest 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 the JDK.

    Java compiler check on ubuntu

    Java compiler not installed

    If the Java Compiler is not installed on your system, install the Java Development Kit using this command:

    sudo apt install default-jdk

    This will install the default Java version on your current Ubuntu version. If you need some other specific 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’s 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.

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

    which Javac

    The problem here is that the location it provides is actually a symbolic link. You will have to follow it a few times:

    get java home ubuntu path

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

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

    readlink -f `which javac` | 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 location of the executable file is /usr/lib/jvm/java-11-openjdk-amd64. It might be different for you. Copy the correct path obtained with the above command to your system. You know, you can copy and paste into Ubuntu terminal.

    Step 3: Set 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

    define java home Ubuntu Linux

    define 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 must add it to the bashrc file in your home directory.

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

    Come back up your bashrc file (in case you mess up, you can recover it):

    cp ~ / .bashrc ~ / .bashrc.bak

    Then use the echo command to append the export command used at the beginning of this section. Change the command below to use the correct path as displayed by your system on.

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

    Check that it was correctly added to the final of the file:

    tail -3 ~ / .bashrc

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

    Here is all the output from the three commands above.

    setting java home bashrc

    Now, even if you log out or restart 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 note

    I hope you like the guide How you can Set JAVA_HOME in Ubuntu Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.

    We will be happy to hear your thoughts

        Leave a reply

        Compsmag
        Logo