Java home linux command

JAVA_HOME directory in Linux

Is there any linux command I could use to find out JAVA_HOME directory? I’ve tried print out the environment variables («env») but I can’t find the directory.

11 Answers 11

On Linux you can run $(dirname $(dirname $(readlink -f $(which javac))))

On Mac you can run $(dirname $(readlink $(which javac)))/java_home

I’m not sure about windows but I imagine where javac would get you pretty close

This is a great solution, but remember to use the canonical flag for readlink as Java can be set up with multiple links, so use $(dirname $(dirname $(readlink -f $(which javac)))) on Linux instead

Thanks @MatthewO’Riordan! I added the -f flag for the linux command. Note that readlink on mac is not the GNU version so I excluded -f for the mac version of the script. Mac users that have the GNU version of readlink installed via brew can use -f .

This is the best solution to find any home paths. I use it frequently for java, maven, and whatnot! Thank you!

Ah, nevermind, the intention is clearer with an echo before it: echo $(dirname $(dirname $(readlink -f $(which javac))))

Just another solution, this one’s cross platform (uses java ), and points you to the location of the jre.

java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home' 

Outputs all of java ‘s current settings, and finds the one called java.home .

For windows, you can go with findstr instead of grep.

java -XshowSettings:properties -version 2>&1 | findstr "java.home" 

I’ve reseached a lot of ways to find JAVA_HOME and I think this is one of the best! Thanks for mentioning it!

It’s probably worth mentioning that this started working with JDK1.7 (IIRC). Before that you would have needed to call: jrunscript -e ‘java.lang.System.out.println(java.lang.System.getProperty(«java.home»));’ . In both cases, for ‘cross-platform’ aspects, it is recommendable to prepend a /usr/bin/env .

echo $JAVA_HOME will print the value if it’s set. However, if you didn’t set it manually in your startup scripts, it probably isn’t set.

If you try which java and it doesn’t find anything, Java may not be installed on your machine, or at least isn’t in your path. Depending on which Linux distribution you have and whether or not you have root access, you can go to http://www.java.com to download the version you need. Then, you can set JAVA_HOME to point to this directory. Remember, that this is just a convention and shouldn’t be used to determine if java is installed or not.

@harschware When I install Ubuntu the JAVA_HOME is not set, even with the JDK installed. Also read /usr/share/doc/openjdk-8-jre-headless/JAVA_HOME and it says the JAVA_HOME should no longer be needed (legacy).

@PeterQuiring Thanks Peter, unfortunately I don’t have easy access to that file. Perhaps there is an effort to deprecate it in Java 8, but the shear number of tools that rely on JAVA_HOME will mandate a need to continue to define the variable for many years yet.

Читайте также:  Установочная флешка через линукс

I know this is late, but this command searches the /usr/ directory to find java for you

/usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-1.6.0-openjdk 

FYI, if you are on a Mac, currently JAVA_HOME is located at

You might want to try sudo find /usr/ -name *jdk* as my path was /usr/lib/jvm/java-7-openjdk-amd64/ .

To show the value of an environment variable you use:

In case you don’t have it setted, you can add in your .bashrc file:

export JAVA_HOME=$(readlink -f /usr/bin/java | sed «s:bin/java::»)

and it will dynamically change when you update your packages.

Unsafe sed command: what if /usr/bin/java was a symlink to /usr/bin/java-8/bin/java ? You’d get the output /usr/-8/bin/java .

Fantastic Solution! if you want to use jdk as home instead of jre just tweak readlink -f /usr/bin/java | sed «s:jre/bin/java::»

If $JAVA_HOME is defined in your environment.

$ echo $JAVA_HOME $ # I am not lucky. 

You can guess it from the classes that are loaded.

$ java -showversion -verbose 2>&1 | head -1 [Opened /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/lib/rt.jar] 

This method ensures you find the correct jdk / jre used in case there are multiple installations.

$ strace -e open java -showversion 2>&1 | grep -m1 /jre/ open("/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY) = -1 ENOENT (No such file or directory) 

If you are not getting anything, then your environment variable JAVA_HOME has not been set. You can try using «locate java» to try and discover where your installation of Java is located.

Did you set your JAVA_HOME

  • Korn and bash shells:export JAVA_HOME=jdk-install-dir
  • Bourne shell:JAVA_HOME=jdk-install-dir;export JAVA_HOME
  • C shell:setenv JAVA_HOME jdk-install-dir

Here’s an improvement, grabbing just the directory to stdout:

java -XshowSettings:properties -version 2>&1 \ | sed '/^[[:space:]]*java\.home/!d;s/^[[:space:]]*java\.home[[:space:]]*=[[:space:]]*//' 

A bit shorter, though with three forks instead of two, is the following: : $&1 | grep «java.home» | cut -d»=» -f2)>

If one needs to export the path directly: export $(/usr/bin/env java -XshowSettings:properties -version 2>&1 | grep «java.home» | sed -e ‘s/java.home/JAVA_HOME/;s/ //g;’)

You can check from the command line by executing this command echo $JAVA_HOME . If Java is installed but the path is not set, you need to identify the path to your java installation. I prefer using sudo update-alternatives —config java which lists all installed versions with current active one marked and provides dialog to switch:

There are 3 programs which provide 'java'. Selection Command ----------------------------------------------- 1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.14.0.9-2.fc35.x86_64/bin/java) 2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.2.0.8-1.fc35.x86_64/bin/java) *+ 3 /usr/java/jdk-17.0.2/bin/java Enter to keep the current selection[+], or type selection number:

from the above list, you can select the version of java you want to be the default. To set the JAVA_HOME to option 3 for instance you can do it this way export JAVA_HOME=/usr/java/jdk-17.0.2

Источник

How to Set JAVA_HOME Variable in Ubuntu Linux Correctly

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

Читайте также:  Мониторинг загрузки системы linux

In this beginner’s tutorial, I’ll show the steps to correctly set Java Home variable on Ubuntu. The steps should be valid for most other Linux distributions as well.

The process consists of these steps:

  • Making sure Java Development Kit (JDK) is installed.
  • Finding the correct location of JDK executable.
  • Setting the JAVA_HOME variable and making the change permanent.

Step 1: Check if JDK is installed

The simplest way to check if Java Development Kit (JDK) is installed on your Linux system is by running this command:

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

check java compiler ubuntu

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

java compiler check ubuntu

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

sudo apt install default-jdk

This will install the default Java version in your current Ubuntu version. If you need some other specific Java version, you’ll have to specify it while installing Java on Ubuntu.

Once you have made sure that Java Compiler is present on your system, it’s time to find its location.

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

The executable is usually located in the /usr/lib/jvm directory. I won’t left you on your own for a guessing game. Instead, let’s find out the path of the Java executable.

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

The problem here is that the location it gives is actually a symbolic link. You’ll have to follow it a couple of times:

get java home path ubuntu

And when you find a path like /usr/lib/jvm/java-11-openjdk-amd64/bin/javac, you remove the /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 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 then used to replace /bin/javac by nothing and thus removing it altogether.

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

Step 3: Setting JAVA_HOME variable

Now that you have got 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 JAVA_HOME directory:

set java home ubuntu linux

Try to run your program or project in 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 JAVA_HOME variable ‘permanently’, you should add it to the bashrc file in your home directory.

Читайте также:  Установка telnet linux centos

You can use the Nano editor for editing files in the Linux terminal. If you do not want that and take a simple copy-paste approach, use the following commands:

Back up your bashrc file (in case you mess it, you can get it back):

Next, use the echo command to append the export command you used at the beginning of this section. Change the command below to use the correct path as displayed by your system in.

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

Verify that it has been correctly added to the end of the file:

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

Here’s the entire output of the above three commands.

setting java home bashrc

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

Do note that if you change the default Java version in the future, you’ll have to change the value of JAVA_HOME and point it to the correct executable path.

I hope this tutorial not only helped you to set Java Home, it also taught you how you are doing it.

If you are still facing issues or have any questions or suggestions, please let me know in the comments.

Источник

How to set JAVA_HOME for Java?

You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment .

Open /etc/environment in any text editor like nano or gedit and add the following line:

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

(java path could be different)

Use source to load the variables, by running this command:

Then check the variable, by running this command:

Usually most linux systems source /etc/environment by default. If your system doesn’t do that add the following line to ~/.bashrc (Thanks @pje)

When i tried to run Android Studio (that has IntelliJ IDEA as a base), i had an error message very similar to @advocate’s: «‘tools.jar’ seems to be not in Android Studio classpath.» After fiddling a lot with JAVA_HOME without success, i decided to take a look at studio.sh, the shellscript that starts Android Studio. As a wild guess, i set JDK_HOME to the same value expected for JAVA_HOME, and voila! It installed without great problems.

For those doing software development, don’t put your JAVA_HOME in /etc/environment unless you want to reboot everytime you switch JDK versions.

This is a temporary solution, as others pointed out. No one would want to run source every time they restart their bash.

As others have pointed out, this doesn’t stick between terminal sessions. What I did to address this is just added the line source /etc/environment to the top of my bash config file ~/.bashrc so that it loads all my environment settings on startup. Working for me so far.

Источник

Оцените статью
Adblock
detector