Linux what is my java home

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.

Читайте также:  Find service name linux

@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

Читайте также:  Linux создать установочную флешку windows

Источник

Find JAVA_HOME and set it on RHEL

I had installed java a while ago on my RHEL machine. Now, I’m trying to run a program that requires the JAVA_HOME variable to be set. What is the best way to figure out the installation directory of my java installation and then set JAVA_HOME ? Here are the results of running java- version :

java version "1.7.0_25" Java(TM) SE Runtime Environment (build 1.7.0_25-b15) Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode) 

7 Answers 7

RHEL uses alternatives subsystem to manage java installations. You can have multiple versions of java installed, but only one is active at a time.

This means that running which java doesn’t provide useful information. The output would be the same no matter which java installation is selected via alternatives. Running readlink -f $(which java) (as already suggested in other comment) or using asking alternatives alternatives —display java would be better.

See example from RHEL 6 machine with OpenJDK installed (which is shipped with RHEL):

[root@example ~]# which java /usr/bin/java [root@example ~]# readlink -f $(which java) /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64/jre/bin/java [root@example ~]# alternatives --display java | head -2 java - status is manual. link currently points to /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java 

Note that enviroment variable JAVA_HOME is not defined anywhere by default, you would need to define it yourself in .bashrc of user which requires it.

In previous example, correct value of JAVA_HOME would be /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64 .

See details in Install OpenJDK documentation, search for section «Optional: Set the JAVA_HOME environment variable».

Источник

Which of these is my JDK path, and which is JAVA_HOME?

enter image description here

echo $PATH —> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

java -version -> java version «1.7.0_03» OpenJDK Runtime Environment (IcedTea7 2.1.1pre) (7~u3-2.1.1~pre1-1ubuntu3) OpenJDK 64-Bit Server VM (build 22.0-b10, mixed mode)

which java -> /usr/bin/java

1 Answer 1

Spring Tool Suite requires you to specify a path to the JDK, and my question is, which one do I choose, and why?; and

This can only be answered according to the needs of your application. Do you need the stability of Java 6 or the features added in Java 7? Are there APIs you intend to use that are only available in Java 7 or are the Java 6 libraries sufficient? Do you have customers that require the use of one JVM over another? Only you can answer these questions.

How do I tell which of these is my JAVA_HOME?; and

You will want to make JAVA_HOME reference the specific JVM environment that you’ve decided on for your project. For example, if you decide on Java 6 you’ll probably want to set your JAVA_HOME to /usr/lib/jvm/java-6-openjdk-amd64.

What’s the difference between the «JDK path» and JAVA_HOME?

The JDK path refers to the Java Development Kit — which includes the java compiler, debugger and other tools associated with developing java programs. JAVA_HOME can reference either the JDK root or the JRE (Java Runtime Environment) root directory when running java programs; but you should, for development purposes, always have JAVA_HOME reference the JDK root.

Читайте также:  Xbox series gamepad linux

Источник

How to find my current JAVA_HOME in ubuntu?

To display JAVA_HOME variable path, type in terminal:

If nothing appears then set it with this:

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

This will differ according to your JDK type and version.

For displaying it again, follow the first command.

Follow JREs from different vendors on the same system, for using different JDK’s or switch between JDK’s.

It gives «/usr/lib/jvm/java-6-sun». But I have installed java 7. When I check it using «java -version» it gives java version «1.7.0_45» Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) Server VM (build 24.45-b08, mixed mode)

then execute second command for setting JAVA_HOME variable. NOTE: JAVA_HOME doesn’t make jdk default, it just makes JAVA_HOME variable set to a path & if you want to use different jdk installed on same machine then check my answer, I have edited it.

@Jax-L But now when I give echo JAVA_HOME it just displays as «JAVA_HOME». The path I gave is not displaying.

export works only until you restart. Or you add export to the .bashrc login script. But the correct way to set such environment variables is in /etc/environment

If you have JDK 1.6 (corresponding to Java 6) or a newer version installed, you should have a program named jrunscript in your PATH . You can use this to find the corresponding JAVA_HOME . Example:

$ jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));' /opt/local/jdk1.7.0_76/jre 

You could set the environment variable like this:

$ export JAVA_HOME="$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')" 

Note that the JRE doesn’t include jrunscript , so this will only work if you install the JDK, not just the JRE.

Источник

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