I need to set the JAVA_HOME to the latest JDK version which is present in the 64_bit folder.
Currently, I am setting this to :
export JAVA_HOME=/opt/java/hotspot/8/64_bit/jdk1.8.0_172
So is there any way it is automatically picked up the latest JDK version because every time I need to change the script when a new version comes.
Thanks
It can be done in a couple of lines of shell script. There are probably a myriad ways to implement this. The following code relies on sort's -V flag to find the latest version.
#!/bin/sh
BASE_DIR=/opt/java/hotspot/8/64_bit/
version=$(ls $BASE_DIR | grep -Eo "([0-9]+\.?){3}(_[0-9]+)?" | sort -Vr | head -1)
if [ -n "$version" ]; then
folder=$(find $BASE_DIR -maxdepth 1 -name "*${version}*")
echo "Setting JAVA_HOME to $folder."
JAVA_HOME="$folder"
export JAVA_HOME
else
echo 1>&2 "No JDK installation found in ${BASE_DIR}!"
exit 1
fi
Automatically set JAVA_HOME for openjdk
Set java version:
sudo update-alternatives --config java
update-java-alternatives --list
update-java-alternatives --set /usr/lib/jvm/java-1.11.0-openjdk-amd64
Automatically set JAVA_HOME
export JAVA_HOME=$(update-alternatives --display java | grep -e "link currently points to" | sed 's/\ link currently points to //' | sed 's/\/bin\/java/\/jre/g')
Related
I am trying to install Java Developer Kit 8 by running the following commands:
sudo apt-get update
sudo apt-get install -y openjdk-8-jdk
echo 'Updating eventually the correct version of Java...'
var=$(java -version 2>&1 | awk -F '"' 'NR==1 {print $2}') # get the actual version
sudo update-alternatives --set java /usr/lib/jvm/jdk${var}/bin/java
The problem is that I am getting the following error:
default: Updating eventually the correct version of Java...
update-alternatives: error: alternative /usr/lib/jvm/jdk1.8.0_312/bin/java for java not registered; not setting
Is there a missing step in the commands?
I'm somewhat confused. You're directly invoking the java command, which will use the java that's already redirected in in the alternatives configuration. You are literally attempting to set the default java to the java that you've just installed? If you don't change anything, then it should already be pointing to this instance of java.
Secondly, you're assuming that the path to java is the one you're specifying there. It doesn't have to be the case. When I create a minimal docker container with openjdk-8-jdk (and dpkg, so update-alternatives is available), then the path to java is:
root#c330511a7cde:/# update-alternatives --list java
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
So the path that you should have been using was this path.
Now I've manually installed a few more java versions, and because of priorities, java-17 is now the default java, so the output is a bit longer:
root#c330511a7cde:/# update-alternatives --list java
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
In this case, you're probably better finding the java-8 path from update-alternatives and setting it that way, so something like:
java_8_path=$(update-alternatives --list java | grep 'java-8-openjdk' | head -1)
if [ -n "$java_8_path" ]; then
sudo update-alternatives --set java "$java_8_path" || echo "darn"
else
echo "could not find java8 in alternatives list" 1>&2
fi
I tried running ./gradlew from an Android project directory, but I get an error of:
ERROR: JAVA_HOME is set to an invalid directory:
/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
Things I've tried:
Navigated to /Library/Java/JavaVirtualMachines. jdk1.8.0_11.jdk exists, but so does jdk1.7.0_79.jdk
which java prints out /usr/bin/java
printenv prints
...
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
JDK_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
...
javac -version prints javac 1.8.0_11
which javac prints /usr/bin/javac
Check if /usr/libexec/java_home exists. If it does then try running
export JAVA_HOME=`/usr/libexec/java_home`
and rerunning your gradlew build. If it works then make it permanent with
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
For me, I got that error no matter what I tried. Deleting the JAVA_HOME var worked for me.
If you see this message in 2021 after upgrading to Android Studio Arctic Fox (2020.3.1) Stable, then the following answer should help to get yourself unblocked.
Open the "Project Structure" window from the "File" menu and you can see that the JDK location is now moved to the Gradle settings.
Now, click on the Gradle Settings link and you can see another window in which the current JDK location is specified.
Now you should edit your ~/.bashrc OR ~/.zshrc to update the value of JAVA_HOME env variable.
That's it!
Now run the source ~/.bashrc OR source ~/.zshrc command or restart your terminal and enjoy running the ./gradlew command in your project.
Cheers!
I have added the following to my .bash_profile to help sort out issues such as this one. This has the added benefit of being able to run setjdk {version} and switch java versions on the fly.
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $#`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
#Default JDK to Java 8
setjdk 1.8
Try this instead form Mac os
export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home"
In macOS, the JDK installation path is/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home. See the official documentation.
If anyone comes here using fish, adding the following line to ~/.config/fish/config.fish solves the problem for me.
set JAVA_HOME (/usr/libexec/java_home -v (java --version | awk 'NR==1{print $2}'))
I am new to Linux system and there seem to be too many Java folders.
java -version gives me:
java version "1.7.0_55"
OpenJDK Runtime Environment (rhel-2.4.7.1.el6_5-x86_64 u55-b13)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
When I am trying to build a Maven project , I am getting error:
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/java/jdk1.7.0_05/bin/java
Could you please tell me which files I need to modify for root as well as not-root user and where exactly is java located?
find /usr/lib/jvm/java-1.x.x-openjdk
vim /etc/profile
Prepend sudo if logged in as not-privileged user, ie. sudo vim
Press 'i' to get in insert mode
add:
export JAVA_HOME="path that you found"
export PATH=$JAVA_HOME/bin:$PATH
logout and login again, reboot, or use source /etc/profile to apply changes immediately in your current shell
For all users, I would recommend creating a file in /etc/profile.d/java_home.sh the following lines
# Set JDK installation directory according to selected Java compiler
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
This will update dynamically and works well with the alternatives system. Do note though that the update will only take place in a new login shell.
You could use /etc/profile or better a file like /etc/profile.d/jdk_home.sh
export JAVA_HOME=/usr/java/jdk1.7.0_05/
You have to remember that this file is only loaded with new login shells.. So after bash -l or a new gnome-session and that it doesn't change with new Java versions.
None of the other answers were "sticking" for me in RHEL 7, even setting JAVA_HOME and PATH directly in /etc/profile or ~/.bash_profile would not work. Each time I tried to check if JAVA_HOME was set, it would come up blank:
$ echo $JAVA_HOME
(<-- no output)
What I had to do was set up a script in /etc/profile.d/jdk_home.sh:
#!/bin/sh
export JAVA_HOME=/opt/ibm/java-x86_64-60/
export PATH=$JAVA_HOME/bin:$PATH
I initially neglected the first line (the #!/bin/sh), and it won't work without it.
Now it's working:
$ echo $JAVA_HOME
/opt/ibm/java-x86_64-60/
Open terminal and type sudo gedit .bashrc
It will ask you your password. After typing the password, it will open the bash file. Then go to end and type:
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
export PATH=$PATH:$JAVA_HOME/bin
Then save the file and exit from file
Above is for a single user. For all users, you have to follow below steps
gedit /etc/profile
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
export PATH=$PATH:$JAVA_HOME/bin
Copy the bin file path you installed
YOUR PATH
open terminal and edit environment file by typing following command,
sudo nano /etc/environment
In this file, add the following line (replacing YOUR_PATH by the just copied path):
JAVA_HOME="YOUR_PATH"
That should be enough to set the environment variable. Now reload this file:
source /etc/environment
now test it by executing:
echo $JAVA_HOME
Doing what Oracle does (as a former Sun Employee I can't get used to that one)
ln -s latestJavaRelease /usr/java/default
Where latestJavaRelease is the version that you want to use
then export JAVA_HOME=/usr/java/default
The answer is given previous posts is valid. But not one answer is complete with respect to:
Changing the /etc/profile is not recommended simply because of the reason (as stated in /etc/profile):
It's NOT a good idea to change this file unless you know what you are doing. It's much better to create a custom.sh shell script in
/etc/profile.d/ to make custom changes to your environment, as this
will prevent the need for merging in future updates.*
So as stated above create /etc/profile.d/custom.sh file for custom changes.
Now, to always keep updated with newer versions of Java being installed, never put the absolute path, instead use:
#if making jdk as java home
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
OR
#if making jre as java home
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")
And remember to have #! /bin/bash on the custom.sh file
First you need to find out which Java is installed in your PC and which one to use.
For that open terminal with root permission.
sudo su
ls /usr/lib/jvm/
Now it will list the available java versions.
Select the listed version.
Copy the path till there.
Now open bashrc
nano ~/.bashrc
add the following commands to the end
export JAVA_HOME="path that you copied"
export PATH=$JAVA_HOME/bin:$PATH
after that save the file and exit by pressing Ctrl+S followed by Ctrl+X
Now run the below command:
source ~/.bashrc
1...Using the short cut Ctlr + Alt + T to open terminal
2...Execute the below command:
echo export JAVA_HOME='$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")' | sudo tee /etc/profile.d/jdk_home.sh > /dev/null
3...(Recommended) Restart your VM / computer. You can use source /etc/source if don't want to restart computer
4...Using the short cut Ctlr + Alt + T to open terminal
5...Verified JAVA_HOME installment with
echo $JAVA_HOME
One-liner copy from flob, credit to them
This is a very simple script to solve the problem
export JAVA_HOME_BIN=`which java`
export JAVA_HOME_DIR=`dirname $JAVA_HOME_BIN`
export JAVA_HOME=`dirname $JAVA_HOME_DIR`
And for testing:
echo $JAVA_HOME
Posting as answer, as I don't have the privilege to comment.
Point to note: follow the accepted answer posted by "That Dave Guy".
After setting the variables, make sure you set the appropriate permissions to the java directory where it's installed.
chmod -R 755 /usr/java
All operational steps(finding java, parent dir, editing file,...) one solution
zFileProfile="/etc/profile"
zJavaHomePath=$(readlink -ze $(which java) | xargs -0 dirname | xargs -0 dirname)
echo $zJavaHomePath
echo "export JAVA_HOME=\"${zJavaHomePath}\"" >> $zFileProfile
echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> $zFileProfile
Result:
# tail -2 $zFileProfile
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64"
export PATH=$PATH:$JAVA_HOME/bin
Explanation:
1) Let's break the full command into pieces
$(readlink -ze $(which java) | xargs -0 dirname | xargs -0 dirname)
2) Find java path from java command
# $(which java)
"/usr/bin/java"
3) Get relative path from symbolic path
# readlink -ze /usr/bin/java
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/java"
4) Get parent path of /usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/java
# readlink -ze /usr/bin/java | xargs -0 dirname
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin"
5) Get parent path of /usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/
# readlink -ze /usr/bin/java | xargs -0 dirname | xargs -0 dirname
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64"
Step 1 - check the current java version by "echo $JAVA_HOME"
Step 2 - vim /etc/profile
Step 3 - At the end of file you will find
export JAVA_HOME, we need to provide the new path here, make sure that it is not relative.
Step 4 - Save and exit :wq
Step 5 - "source /etc/profile/", this would execute the change
Step 6 - Again do a echo $JAVA_HOME - change would have been reflected.
Probably a good idea to source whatever profile you edit to save having to use a fresh login.
either:
source /etc/
or
. /etc/
Where is whatever profile you edited.
On Linux I add this line to my ~/.profile:
export JAVA_HOME=$(readlink -ze /usr/bin/javac | xargs -0 dirname -z | xargs -0 dirname)
While we are up to setting JAVA_HOME, let me share some benefits of setting JAVA_HOME or any other environment variable:
1) It's easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download new version and make sure your JAVA_HOME points to new version of Java. This is best benefit of using environment variable or links.
2) JAVA_HOME variable is short and concise instead of full path to JDK installation directory.
3) JAVA_HOME variable is platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on respective operating system.
Read more: http://javarevisited.blogspot.com/2012/02/how-to-set-javahome-environment-in.html#ixzz4BWmaYIjH
Use SDKMAN sdkman.io to switch btw. your sdk's.
It sets the JAVA_HOME for you.
open kafka-run-class.sh with sudo to write
you can find kafka-run-class.sh in your kafka folder : kafka/bin/kafka-run-class.sh
check for these lines
Modify the JAVA variable in the else part to point to the java executable in your java/bin. like JAVA="$JAVA_HOME/java"
In /etc/profile , if you open that will you’ll get to know that IT IS no recommended to write on that file. Instead of that make a script of your commands(suppose test.sh)go to /etc/profile.d folder and Put test.sh there. Every time you instance reboot it’ll be automatically called by /etc/profile.
Using vim might be a bit difficult for new user. We can use gedit text editor instead.
Find /usr/lib/jvm/java-1.x.x-openjdk
Enter "gedit /etc/profile" or use "sudo gedit /etc/profile" if logged in as not-privileged
Add the following at the end of line:
export JAVA_HOME="path that you found"
export PATH=$JAVA_HOME/bin:$PATH
Enter "source /etc/profile" in your current shell to apply the changes
I use the line:
export JAVA_HOME=$(readlink -f $(dirname $(readlink -f $(which java) ))/../)
to my ~/.profile so it uses the base of the default java directory at login time. This is for bash.
Try this if doesn't work:
apt install openjdk-8-jdk-headless
alternate --config is updated
java -version works
How do i verify if java is installed if java is installed and what version. What is the correct way.
I think this is what you want:
#!/bin/bash
if [ -x /usr/bin/java ] ; then
java -version 2>&1 | head -1 | awk -F '"' '{print $2}'
else
exit
fi
OUTPUT (on my linux box):
1.6.0_18
check whether the java executable is present. if yes, then print the version.
I hope the following code may help you, please try it,
a=`(java -version) 2>&1`
if [[ "$a" == *1.7* ]]
then
echo '1.7'
fi
Depends on how you expect it to be installed; there are two ways to find the java binary:
look for java in PATH (which is what you're doing by executing java -version)
if JAVA_HOME is defined, treat that as the preferred installation and run $JAVA_HOME/bin/java -version
After that, just extract the version number from the output as you need (see slayedbylucifer's answer above).
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.
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
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"
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.
I know this is late, but this command searches the /usr/ directory to find java for you
sudo find /usr/ -name *jdk
Results to
/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
/System/Library/Frameworks/JavaVM.framework/Home
To show the value of an environment variable you use:
echo $VARIABLE
so in your case will be:
echo $JAVA_HOME
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.
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.
Or using strace:
$ 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)
On the Terminal, type:
echo "$JAVA_HOME"
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:]]*//'
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
http://www.gnu.org/software/sed/manual/html_node/Print-bash-environment.html#Print-bash-environment
If you really want to get some info about your BASH put that script in your .bashrc and watch it fly by. You can scroll around and look it over.