Related
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')
I installed java and set the path environment and when I run echo $JAVA_HOME in the terminal I get the following output:
/usr/lib/jvm/java-7-oracle/jre/bin/java
I Also installed apache-maven and changed environment file and now it looks like this:
JAVA_HOME="/usr/lib/jvm/java-7-oracle/jre/bin/java"
M2_HOME=/usr/local/apache-maven/apache-maven-3.0.5
M2=$M2_HOME/bin
MAVEN_OPTS="-Xms256m -Xmx512m"
PATH=$M2:$PATH
But when I execute mvn --version I get a warning:
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/lib/jvm/java-7-oracle/jre/bin/java/bin/java
Can not find out why it repeats in the end /bin/java/bin/java
Assuming you use bash shell and installed Java with the Oracle installer, you could add the following to your .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/jre/bin:$PATH
This would pick the correct JAVA_HOME as defined by the Oracle installer and will set it first in your $PATH making sure it is found.
Also, you don't need to change it later when updating Java.
EDIT
As per the comments:
Making it persistent after a reboot
Just add those lines in the shell configuration file. (Assuming it's bash)
Ex: .bashrc, .bash_profile or .profile (for ubuntu)
Using a custom Java installation
Set JAVA_HOME to the root folder of the custom Java installation path without the $().
Ex: JAVA_HOME=/opt/java/openjdk
JAVA_HOME should be /usr/lib/jvm/java-7-oracle/jre/.
We open a terminal and look for the location of java:
manuel#zonademanel:~ → whereis java
java: /usr/bin/java /etc/java /usr/bin/X11/java /usr/share/java
/usr/share/man/man1/java.1.gz
What we are looking for is /usr/bin/java continue on the command line to find the absolute path , as this is only a symbolic link.
manuel#zonademanel:~ → ls -lah /usr/bin/java
lrwxrwxrwx 1 root root 22 may 19 2015 /usr/bin/java ->
/etc/alternatives/java
manuel#zonademanel:~ → ls -lah /etc/alternatives/java
lrwxrwxrwx 1 root root 39 dic 7 11:52 /etc/alternatives/java ->
/usr/lib/jvm/java-8-oracle/jre/bin/java
I modified my /etc/environment file with the following values
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-8-oracle/jre/bin"
JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre"
If I do not want to relogin I can reload the changes with:
source /etc/environment
And run mvn -version correctly
manuel#zonademanel:~ → mvn -version
Apache Maven 3.0.5
Maven home: /usr/share/maven
Java version: 1.8.0_77, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: es_MX, platform encoding: UTF-8
OS name: "linux", version: "3.16.0-70-generic", arch: "amd64", family: "unix"
$JAVA_HOME should be the directory where java was installed, not one of its parts:
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
You might get this error due to couple of reasons. To fix this quickly please follow below steps,
First find the java location. To get a list of your installed Java platforms, run the following command from the terminal:
$ sudo update-alternatives --config java
Now set JAVA_HOME and PATH,
$ export JAVA_HOME=<java_home>
$ export PATH=$JAVA_HOME/jre/bin:$PATH
Create the symlink
$ sudo ln -s <java_home>/jre <java_symlink_path>
When we take your case as a example :
$ sudo ln -s /usr/lib/jvm/java-7-oracle/jre /usr/lib/jvm/java-7-oracle/jre/bin/java
Above command will create the symlink location where the system is trying to find in your issue.
Finally do the
$ mvn --version
You should set as it is export JAVA_HOME=/usr/java/jdk1.8.0_31 and run with sudo it will execute as shown below.
Example
sudo mvn -v
OUTPUT
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 11.0.10, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-70-generic", arch: "amd64", family: "unix"
Firstly, in a development mode, you should use JDK instead of the JRE.
Secondly, the JAVA_HOME is where you install Java and where all the others frameworks will search for what they need (JRE,javac,...)
So if you set
JAVA_HOME=/usr/lib/jvm/java-7-oracle/jre/bin/java
when you run a "mvn" command, Maven will try to access to the java by adding /bin/java, thinking that the JAVA_HOME is in the root directory of Java installation.
But setting
JAVA_HOME=/usr/lib/jvm/java-7-oracle/
Maven will access add bin/java then it will work just fine.
This solution work for me...
just type
export PATH=$JAVA_HOME/jre/bin:$PATH in the terminal
then run mvn -version
it will show the same error but with a log like this
which: no javac in (/jre/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/puppetlabs/bin)
Warning: JAVA_HOME environment variable is not set.
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T22:59:23+05:30)
Maven home: /opt/apache-maven-3.2.5
Java version: 1.8.0_171, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-693.el7.x86_64", arch: "amd64", family: "unix"
now copy the Java home path i.e. /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre in my case.
now type,
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre
and the error gets resolve.
NOTE: paste your own path which is shown by your machine in mvn log at export JAVA_HOME.
You must take the whole directory where java is installed, in my case:
export JAVA_HOME=/usr/java/jdk1.8.0_31
It happens because of the reason mentioned below :
If you see the mvn script: The code fails here ---
Steps for debugging and fixing:
Step 1: Open the mvn script /Users/Username/apache-maven-3.0.5/bin/mvn
(Open with the less command like: less /Users/Username/apache-maven-3.0.5/bin/mvn)
Step 2: Find out the below code in the script:
if [ -z "$JAVACMD" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
else
JAVACMD="`which java`"
fi
fi
if [ ! -x "$JAVACMD" ] ; then
echo "Error: JAVA_HOME is not defined correctly."
echo " We cannot execute $JAVACMD"
exit 1
fi
Step3: It is happening because JAVACMD variable was not set. So it displays the error.
Note: To Fix it
export JAVACMD=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/bin/java
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/
Key: If you want it to be permanent
open emacs .profile
post the commands
and press Ctrl-x Ctrl-c ( save-buffers-kill-terminal ).
If you are using mac-OS , export JAVA_HOME=/usr/libexec/java_home need to be changed to export JAVA_HOME=$(/usr/libexec/java_home) .
Steps to do this :
$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
$ source .bash_profile
where /usr/libexec/java_home is the path of your jvm
Use these two commands (for Java 8):
sudo update-java-alternatives --set java-8-oracle
java -XshowSettings 2>&1 | grep -e 'java.home' | awk '{print "JAVA_HOME="$3}' | sed "s/\/jre//g" >> /etc/environment
In my case, the problem was in vscode.
I use windows and installed Ubuntu on my computer, so vscode terminal uses the Ubuntu. I just go to settings.json and change to cmd.exe again
I had this same issue but with open jdk and none of the answers here helped. The trouble was that the mvn script was appending /bin/java at the end of JAVA home while trying to run java commands.
The solution for me was to manually edit the /usr/local/apache-maven/apache-maven-3.3.9/bin/mvn script (your own script might be installed differently; just run which mvn) and change
if [ -z "$JAVACMD" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
else
JAVACMD="`which java`"
fi
fi
To
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME"
fi
else
JAVACMD="`which java`"
fi
fi
add in .bash_profile
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/jre/bin:$PATH
and then
source .bash_profile
I had similar issue and it was not getting resolved even after editing and sourcing the .bash_profile, .bashrc file. I got it resolved by editing the maven.sh file directly.
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.16.0.8-1.amzn2.0.1.x86_64
export M2_HOME=/opt/maven
export M2=/opt/maven/bin
export PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2HOME:$M2
and then sourcing the file
source /etc/profile.d/maven.sh
For those who didnt get they issue resolved with other answers, give this a try as well.
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 just installed JDK in Ubuntu with sudo apt-get install openjdk-6-jdk command,
after the installation where's the Java bin directory located? And how can I set the environment path for that directory? I have little experience with Ubuntu, can anyone give some advice or suggest any good website for reference?
set environment variables as follows
Edit the system Path file /etc/profile
sudo gedit /etc/profile
Add following lines in end
JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
Then Log out and Log in ubuntu for setting up the paths...
Java is typically installed in /usr/java
locate the version you have and then do the following:
Assuming you are using bash (if you are just starting off, i recommend bash over other shells) you can simply type in bash to start it.
Edit your ~/.bashrc file and add the paths as follows:
for eg. vi ~/.bashrc
insert following lines:
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
after you save the changes, exit and restart your bash or just type in bash to start a new shell
Type in export to ensure paths are right.
Type in java -version to ensure Java is accessible.
Ubuntu installs openjdk6 to /usr/lib/jvm/java-6-openjdk path. So you will have the bin in /usr/lib/jvm/java-6-openjdk/bin. Usually the classpath is automatically set for the java & related executables.
To Set JAVA_HOME / PATH for a single user, Login to your account and open .bash_profile file
$ vi ~/.bash_profile
Set JAVA_HOME as follows using syntax export JAVA_HOME=<path-to-java>. If your path is set to /usr/java/jdk1.5.0_07/bin/java, set it as follows:
export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
Set PATH as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
Feel free to replace /usr/java/jdk1.5.0_07 as per your setup. Save and close the file. Just logout and login back to see new changes. Alternatively, type the following command to activate the new path settings immediately:
$ source ~/.bash_profile
OR
$ . ~/.bash_profile
Verify new settings:
$ echo $JAVA_HOME
$ echo $PATH
Tip: Use the following command to find out exact path to which java executable under UNIX / Linux:
$ which java
Please note that the file ~/.bashrc is similar, with the exception that ~/.bash_profile runs only for Bash login shells and .bashrc runs for every new Bash shell.
To Set JAVA_HOME / PATH for all user, You need to setup global config in /etc/profile OR /etc/bash.bashrc file for all users:
# vi /etc/profile
Next setup PATH / JAVA_PATH variables as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
Save and close the file. Once again you need to type the following command to activate the path settings immediately:
# source /etc/profile
OR
# . /etc/profile
You need to set the $JAVA_HOME variable.
In my case while setting up Maven, I had to set it up to where JDK is installed.
First find out where JAVA is installed:
$ whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
Now dig deeper:
$ ls -l /usr/bin/java
lrwxrwxrwx 1 root root 46 Aug 25 2018 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Dig deeper:
$ ls -l /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
-rwxr-xr-x 1 root root 6464 Mar 14 18:28 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
As it is not being referenced to any other directory, we'll use this.
Open /etc/environment using nano:
$ sudo nano /etc/environment
Append the following lines
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
export JAVA_HOME
Reload PATH using:
$. /etc/environment
Now:
$ echo $JAVA_HOME
Here is your output:
/usr/lib/jvm/java-1.8.0-openjdk-amd64
Sources I referred to:
https://askubuntu.com/a/175519
https://stackoverflow.com/a/23427862/6297483
if you have intalled only openJDK, the you should update your links, because you can have some OpenJDK intallation.
sudo update-alternatives --config java
after this
$gedit ~/.bashrc
add the following line in the file
JAVA_HOME=/usr/lib/jvm/YOUR_JAVA_VERSION
export PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME
you can get you java version with
java -version
Open terminal (Ctrl+Alt+t)
Type
sudo gedit .bashrc
Enter password of ubuntu user
Go to last line of the file
Type below code in new line
export JAVA_HOME=enter_java_path_here
export PATH=$JAVA_HOME/bin:$PATH
eg: export JAVA_HOME=/home/pranav/jdk1.8.0_131
export PATH=$JAVA_HOME/bin:$PATH
Save the file
Type
source ~/.bashrc
in terminal
Done
To set up system wide scope you need to use the
/etc/environment file sudo gedit /etc/environment
is the location where you can define any environment variable. It can be visible in the whole system scope. After variable is defined system need to be restarted.
EXAMPLE :
sudo gedit /etc/environment
Add like following :
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME="/opt/jdk1.6.0_45/"
Here is the site you can find more : http://peesquare.com/blogs/environment-variable-setup-on-ubuntu/
How to install java packages:
Install desired java version / versions using official ubuntu packages, which are managed using alternatives:
sudo apt install -y openjdk-8-jdk
or/and other version:
sudo apt install -y openjdk-11-jdk
Above answers are correct only when you have only one version for all software on your machine, and you can skip using update-alternatives. So one can quickly hardcode it in .bashrc or some other place:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
but it's not healthy, as later on you may change the version.
Correct way to set JAVA_HOME (and optionally JAVA_SDK, JAVA_JRE )
The correct way (and mandatory when you have more than one), is to detect what update-alternative is pointing to, and always use update-alternatives to switch active version.
Here are the suggestions for both: only specific unix account or for all accounts (machine level).
1. for a specific unix account only:
Use this if you don't have permissions to do it at machine level.
cat <<'EOF' >>~/.bashrc
export JAVA_HOME=$(update-alternatives --query java | grep Value | cut -d" " -f2 | sed 's!\(\/.*\)jre\(.*\)!\1!g')
export JDK_HOME=${JAVA_HOME}
export JRE_HOME=${JDK_HOME}/jre/
EOF
2. To do it at machine level, and for all bourne shells, you need 2 steps:
2.a
cat <<'EOF' | sudo tee /etc/profile.d/java_home_env.sh >/dev/null
export JAVA_HOME=$(update-alternatives --query java | grep Value | cut -d" " -f2 | sed 's!\(\/.*\)jre\(.*\)!\1!g')
export JDK_HOME=${JAVA_HOME}
export JRE_HOME=${JDK_HOME}/jre/
EOF
As your shell might not be set as interactive by default, you may want to do this also:
2.b
cat <<'EOF' | sudo tee -a /etc/bash.bashrc >/dev/null
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
EOF
PS: There should be no need to update the $PATH, as update-alternatives takes care of the link to /usr/bin/.
More on: https://manpages.ubuntu.com/manpages/trusty/man8/update-alternatives.8.html
It should put java in your path, probably in /usr/bin/java. The easiest way to find it is to open a term and type which java.
Create/Open ~/.bashrc file $vim ~/.bashrc
Add JAVA_HOME and PATH as referring to your JDK path
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
Save file
Now type java -version it should display what you set in .bashrc file.
This will persist over sessions as well.
Example :
Update bashrc file to add JAVA_HOME
sudo nano ~/.bashrc
Add JAVA_HOME to bashrc file.
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
Ensure Java is accessible
java -version
In Case of Manual installation of JDK, If you got an error as shown below
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Execute the following command in your JAVA_HOME/lib directory:
unpack200 -r -v -l "" tools.pack tools.jar
Execute the following commands in your JAVA_HOME/jre/lib
../../bin/unpack200 rt.pack rt.jar
../../bin/unpack200 jsse.pack jsse.rar
../../bin/unpack200 charsets.pack charsets.jar
Ensure Java is accessible
java -version
I have a Linux Lite 3.8 (It bases on Ubuntu 16.04 LTS) and a path change in the following file (with root privileges) with restart has helped.
/etc/profile.d/jdk.sh
Step1:
sudo gedit ~/.bash_profile
Step2:
JAVA_HOME=/home/user/tool/jdk-8u201-linux-x64/jdk1.8.0_201
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
Step3:
source ~/.bash_profile
Let me simplify:
download JDK from Oracle Website : Link
Extract it
Create a folder (jvm) in /usr/lib/ i.e /usr/lib/jvm
move the extracted folder from the jdk to /usr/lib/jvm/
*Note : use terminal, sudo, mv command i.e. sudo mv
Create a .sh file at /etc/profile.d/ eg: /etc/profile.d/myenvvar.sh
In the .sh file type
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0
export PATH=$PATH:$JAVA_HOME/bin
*Note : use terminal, gedit and sudo eg: sudo gedit myenvvar.sh
Turn Off the Computer, after all these steps and Restart it
Open Terminal , and type
java -version
Check the output , then type
echo $JAVA_HOME
Once I've installed the openjdk version of the Java Development Kit on an Ubuntu machine, I use this procedure to create a JAVA_HOME environment variable that doesn't need to be changed after every version upgrade of the openjdk installation.
Firstly, I issue a command to discover the directory in which the java executables are located for this java installation.
echo $(readlink -e `which java` | xargs -0 dirname)
If I'm happy with the output from that, everything else can be derived from it.
Configuring the JAVA_HOME and PATH environment variables
Rather than adding more and more configurations to the ~/.bashrc file I've found it a cleaner practice to create a separate small file that ~/.bashrc can "include" when it runs.
Let's call that file ~/.java_env_vars (but you could name it whatever you wish).
Add an "include condition" to the ~/.bashrc file
Open ~/.bashrc in any text editor and these lines to the end of the file:
# include the java environment configuration file here (if it exists)
if [ -f "$HOME/.java_env_vars" ]; then
. $HOME/.java_env_vars
fi
Create the Java environment variable configuration file
Open any text editor, create the Java environment configuration file ~/.java_env_vars and add this content to it:
#1. set a java_bin variable to the directory containing the actual Java executables.
java_bin=$(readlink -e `which java` | xargs -0 dirname)
#2. append "$java_bin" to the PATH environment variable
export PATH=$PATH:"$java_bin"
#3. assign the directory of the current Java installation to the JAVA_HOME environment variable.
export JAVA_HOME=$(dirname "$java_bin")
NOTE: exporting the PATH and JAVA_HOME variables just ensures that they're always available wherever they're needed.
Source your ~/.bashrc file (so that the changes you made to it are reflected) using the following command:
source ~/.bashrc
Test the Java environment variable configuration
Open a new terminal console, and test the new Java environment variables by issuing this command:
echo "$PATH" && echo "$JAVA_HOME"
Output should be two lines, something like this:
/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-11-openjdk-amd64/bin
/usr/lib/jvm/java-11-openjdk-amd64
All you have to do now is to set the “JAVA_HOME” and “PATH” environment variables and then you are done. Enter the following commands to set your environment variables. Make sure that your environment variables point to a valid installation of JDK on your machine. For Ubuntu 18.04, the path is /usr/lib/jvm/java-8-openjdk-amd64/
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
To check whether your JAVA_HOME path has been successfully saved, enter the following command to check.
echo $JAVA_HOME
Open file /etc/environment with a text editor
Add the line JAVA_HOME="[path to your java]"
Save and close then run source /etc/environment
Set java version from the list of installed. For see the list of the installed version run following command:
update-java-alternatives --list
Then set your java version according to the following command:
sudo update-java-alternatives --set /usr/lib/jvm/java-1.8.0-openjdk-amd64
open jdk once installed resides generally in your /usr/lib/java-6-openjdk
As usual you would need to set the JAVA_HOME, classpath and Path.
In ubuntu 11.04 there is a environment file available in /etc where you need to set all the three paths. And then you would need to restart your system for the changes to take effect..
Here is a site to help you around
http://aliolci.blogspot.com/2011/05/ubuntu-1104-set-new-environment.html
Once JDK installed set the JAVA_HOME in environment
sudo nano /etc/environment and add the line JAVA_HOME="/usr/lib/jvm/jdk-11.0.1/"
Add the configuration in .bashrc
sudo nano ~/.bashrc and add following lines
JAVA_HOME=/usr/lib/jvm/jdk-11.0.11/
PATH=$JAVA_HOME/bin:$PATH
refresh the new configuration with source ~/.bashrc
enter the command java -version and you can see the version installed on your machine
You can install the default Ubuntu(17.10) java from apt:
sudo apt install openjdk-8-jdk-headless
And it will set the PATH for you, if instead you need to install specific version of Java you can follow this YouTube
I installed java 11 in my Ubuntu 20.04. Setting up a JAVA_HOME for the same.
enter the this command to find out your ubuntu version --
swapnil#swapnil-vm:~$ lsb_release -d
Description: Ubuntu 20.04.3 LTS
enter this command to find out the location of your jvm --
swapnil#swapnil-vm:~$ whereis jvm
jvm: /usr/lib/jvm
open .bashrc in any editor of your choice --
nano .bashrc
add the following lines --
## setting JAVA_HOME
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
now you are good to go!!
open a new terminal and enter the command --
ehco $JAVA_HOME
Use the following lines to set the path variables in the /etc/environment
echo export JAVA_HOME=/path/to/java | sudo tee -a /etc/environment
echo export JRE_HOME=/path/to/jre | sudo tee -a /etc/environment
It should work.
Note:
You should reboot the system for changes to take effect.
Installation of Oracle Java:
Download the tarball (.tar file) from Oracle website
unzip it by sudo tar -xvpzf fileName -C /installation_folder_name
change the files permission and ownership
add the following two lines in /etc/profile
export JAVA_HOME=/home/abu/Java/jdk1.8.0_45/
export PATH=$JAVA_HOME/bin:$PATH
restart the machine and check by java -version and javac -version
First, check whether env var exists or not
echo $JAVA_HOME
if an env var exists with that name then the above command will return the env var Path. if it's return nothing then copy the env path first then execute below command. such as my java env path is /usr/lib/jvm/java-11-openjdk-amd64
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
I used to have multiple JDK installed on my Linux machine, and I like to switch from one version to another from the command-line ( I used to change my JAVA_HOME manually):
This is my current approach :
I source a ~/.paths in my .bashrc.
the .paths contain all the JDK installed on my machine.
JDK7="~/local/jdk1.7.0_15"
JDK8="~/local/jdk1.8.0"
// I use Jdk 7 by default
JDK_HOME=$JDK7;
// including $JDK_HOME/bin to the $PATH
When I want t switch to JDK8, I modify the JDK_HOME variable to point on JDK8 in the file, and I re-source my .paths file.
I know that IDE can manage multiple JDK easily, but I want an rvm like solution.
Is there any better trick ?
Better more, Is there any equivalent for rvm in Java ?
There is jdk_switcher although it is quite static to some ubuntu paths - it should be easy to modify it to run from other paths.
There is a plan to make RVM 2 support switching more then just Ruby versions, you can read more about it here.
anyone with the link can comment.
I don't think there is such solution.
See this question to a solution with symlinks.
Now , I'm using jdk-manager, a little bash script to manage multiple JDKS installations.
You can have as many Java versions installed as you want to. Just install to a folder of your choosing and use some convention.
If you want to make one-time use of a particular version run it with a full path (for example):
>C:\java\jdk-6u35\bin\java.exe
or
>/java/jdk-6u35/bin/java.exe
If you want to change to just use it, change your path to put the version you want at the front. The path might be similar to that shown above.
Be sure to change JAVA_HOME as well and any other environment settings that include a reference to the java location.
Note that some tools have internal configuration as to which java version to use. Eclipse is a good example. You have to set up a list of your JVMs and then select one for each project or for all projects.
I wrote my own script to manage java versions. I use some sites that require Oracle Java and usually the latest version, so I can't use apt/aptitude and therefore can't use update-alternatives or jdk-manager (which uses update-alternatives).
Here's my script. I don't develop professionally, so it's probably a mess, but it serves my purposes. It only considers java versions stored in /usr/lib/jvm.
#!/bin/bash
# the proper way....
# + update: update-alternatives only works for versions installed by apt or aptitude
# ... same for jdk-manager (uses update-alternatives to do the heavy lifting)
#echo "The proper way is:"
#echo "$ update-alternatives --config java"
#exit
# the rest is (no longer) depreciated....
echo "The current default java is $(readlink --canonicalize `which java`)"
PS3='Select Java to install: '
options=( $(find /usr/lib/jvm -iname java) )
noptions=${#options[#]}
(( loption=noptions-1 ))
options[${#options[#]}]="Quit"
select opt in "${options[#]}"
do
for i in $(seq 0 $loption); do
[ "$opt" == "${options[$i]}" ] && \
javapath=${options[$i]}
done
if [ "$javapath" ]; then
break
fi
if [ "$opt" == "Quit" ]; then
echo "Nothing installed.";
exit
else
echo "Invalid option. Try another one.";
continue
fi
done
# remove the old link (might be superfluous)
#rm -vf -- "$link"
# set new link (symbolic, force, verbose)
sudo ln -sTfv "$javapath" "/usr/bin/java"
default_java_dir=$(echo "$javapath" | grep --only-matching --regexp="\/usr\/lib\/jvm\/[^\/]*")
sudo ln -sTfv "$default_java_dir" "/usr/lib/jvm/default-java"
java_bin_dir=$(echo "$javapath" | sed 's/[^\/]*$//')
echo $java_bin_dir
[ -f "${java_bin_dir}javac" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javac"
[ -f "${java_bin_dir}javadoc" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javadoc"
[ -f "${java_bin_dir}javafxpackager" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javafxpackager"
[ -f "${java_bin_dir}javah" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javah"
[ -f "${java_bin_dir}javap" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javap"
[ -f "${java_bin_dir}java-rmi.cgi" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}java-rmi.cgi"
[ -f "${java_bin_dir}java_vm" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}java_vm"
[ -f "${java_bin_dir}javaws" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javaws"
find_dir=$(dirname "$java_bin_dir")
pluginpath=$(find "$find_dir" -name libnpjp2.so)
#exit
echo -n "Install $pluginpath as the Java plugin? [y/N]: "
read
response=$(echo $REPLY | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
if [ "${response:0:1}" == "Y" ]; then
# directories for plugins
plugin_dirs="/usr/lib/firefox/plugins
/usr/lib/firefox-addons/plugins
/usr/lib/mozilla/plugins
/opt/google/chrome
/home/james/.mozilla/plugins"
# first, clean out anything we might have improperly installed already
echo "Using 'sudo' to remove any installed java plugins..."
for pdir in $plugin_dirs; do
sudo rm --verbose --force "$pdir/libjavaplugin_oji.so" "$pdir/libnpjp2.so" "$pdir/IcedTeaPlugin.so"
done
# okay, trying brute force and awkwardness....
echo "Using 'sudo' to install \"$pluginpath\" in several places..."
for pdir in $plugin_dirs; do
sudo ln --symbolic --verbose --force "$pluginpath" "$pdir"
done
fi
exit
If you use Debian or a derived GNU/Linux distribution, you can use update alternatives to set what is currently being run when you type java.
Try typing
update-alternatives --display java
That will show you what alternatives you have available.
This command is non-destructive, that is, it doesn't change anything.
man update-alternatives
Will give you the manual for the tool.
The command you will likely want though is:
update-alternatives --config java
Which will give you a simple, interactive way of setting the java program. You can also use --set if you want to script it.
Of course, you should not trust me without running man first, because people sometimes go on the Internet and tell lies. ;)
EDIT: I forgot, this link introduces update-alternatives in a good (if Vi specific) way.
For Linux, you can use update-alternatives to not only set paths to java, javac, and other binaries but also your JAVA_HOME. Since all it does is manage links, you can install a link to your jdk directories and then set JAVA_HOME to point to that link. For example, "update-alternatives --install /usr/lib/jdk jdk /path/to/jdk8 1" will install a link to your jdk directory. You then add "export JAVA_HOME to /usr/lib/jdk" in .bashrc, .profile, or whatever file you use to set environment variables and any alternative you install under the name jdk will be pointed to by JAVA_HOME when you switch with update-alternatives --config jdk. If you are using alternatives already for the java executables, you can use --slave to make jdk whenever java does.