How to find my current JAVA_HOME in ubuntu?
Sophia Terry
How to find my current JAVA_HOME in ubuntu? I have to set java_home path when installing maven.
9 Answers
To display JAVA_HOME variable path, type in terminal:
echo $JAVA_HOMEIf nothing appears then set it with this:
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64This 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.
9If 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/jreYou 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.
Another portable options is to extract the absolute path of the JRE from java:
export JAVA_HOME=`type -p java|xargs readlink -f|xargs dirname|xargs dirname`The absolute java path is passed to dirname twice to remove /bin/java from the end. Complete extraction of the directory goes as follows:
$ type -p java
/usr/bin/java
$ readlink -f /usr/bin/java
/usr/lib/jvm/java-8-oracle/bin/java
$ dirname /usr/lib/jvm/java-8-oracle/bin/java
/usr/lib/jvm/java-8-oracle/bin/
$ dirname /usr/lib/jvm/java-8-oracle/bin/
/usr/lib/jvm/java-8-oracle/ 4 To take into account the update-alternatives mechanism:
$ update-alternatives --query java | grep 'Value: ' | grep -o '/.*/jre'You could set the environment variable like this:
$ export JAVA_HOME="$(update-alternatives --query java | grep 'Value: ' | grep -o '/.*/jre')" 1 Just run a command
sudo update-alternatives --config javaIt will give something like
Es gibt nur eine Alternative in Link-Gruppe java (die /usr/bin/java bereitstellt): /usr/lib/jvm/java-8-oracle/jre/bin/javaFrom this you have /usr/lib/jvm/java-8-oracle/ as java home. You may now export it to JAVA_HOME variable
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/Now echo $JAVA_HOME show it
I use this in Ubuntu LTS (14.04 / 16.04):
[ -L /etc/alternatives/java ] && export JAVA_HOME="$(readlink -f /etc/alternatives/java | sed -e 's/\/jre\/bin\/java$//')" For Java 9 and later:
This answer uses the enclosed Nashorn JavaScript engine Nashorn to print out the java.home system property. Nashorn is being deprecated so an alternative is to use jshell introduced in Java 9.
echo 'System.out.println(java.lang.System.getProperty("java.home"));' | jshell -which on my Ubuntu 18.10 system prints out:
/usr/lib/jvm/java-11-openjdk-amd64 Set Java environment variables
The PPA also contains a package to automatically set Java environment variables, just run command:
sudo apt install oracle-java8-set-defaultFrom this article:Install Oracle Java 8 / 9 in Ubuntu 16.04, Linux Mint 18
to get JAVA_HOME:
update-alternatives --query java | grep 'Value: ' | sed 's/Value: \(.*\)\/bin\/java/\1/'
## e.g. returns </opt/Oracle_Java/jre1.8.0_202>
## if </etc/alternatives/java> points to
## </opt/Oracle_Java/jre1.8.0_202/bin/java>to set JAVA_HOME:
export JAVA_HOME="$(update-alternatives --query java | grep 'Value: ' | sed 's/Value: \(.*\)\/bin\/java/\1/')"