Is there any Java version manager? - java

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.

Related

update-alternatives: error: alternative /usr/lib/jvm/jdk1.8.0_312/bin/java for java not registered; not setting

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

Install Java7 on Ubuntu 16.04 through bash script

I have below function java_install written in a bash script to install java on Linux box, to which I pass jdk-1.7.0_80-linux-x64.tgz as JAVA_PACKAGE.
Now what is happening is java gets installed and works fine only within the script. Once I come out of this script, none of the java functionalities work, not even java -version. Could someone please help me on what I might be missing here? Basically, I just want java to be installed permanently on this box once this script is executed.
java_install() {
local JAVA_PACKAGE=$1
local TMPDIR=/tmp/quickstart
local TARGET=/usr/share
if [ -n "$JAVA_PACKAGE" ] && [ -f "$JAVA_PACKAGE" ]; then
rm -rf $TMPDIR
mkdir -p $TMPDIR
cp $JAVA_PACKAGE $TMPDIR
( cd $TMPDIR && tar fxz $JAVA_PACKAGE && rm $JAVA_PACKAGE )
local JAVA_BASENAME=$(ls -1 $TMPDIR)
mkdir -p $TARGET
if [ -d "$TARGET/$JAVA_BASENAME" ]; then
echo "# Java already installed at $TARGET/$JAVA_BASENAME"
log_info "Java already installed at $TARGET/$JAVA_BASENAME"
else
echo "# Java now installed at $TARGET/$JAVA_BASENAME"
log_info "Java now installed at $TARGET/$JAVA_BASENAME"
mv $TMPDIR/$JAVA_BASENAME $TARGET
fi
rm -rf $TMPDIR
# now create a script to export these settings
export JAVA_HOME=$TARGET/$JAVA_BASENAME
export PATH=$JAVA_HOME/bin:$PATH
else
echo "# cannot find java package to install"
log_error "cannot find java package to install"
fi
}
Use update alternatives within your script to make your java installation available:
sudo update-alternatives --install "/usr/bin/java" "java" "path to you java executable" 1
More information on this topic can be found here: How to use the command update-alternatives --config java.
Alternatively you can write the export commands for JAVA HOME and PATH to your .bashrc from within your script (if using bash). This way the modified variables are available in the bash shell.

Can Java 7 and Java 8 co-exist on OSX

I've installed Java 8 for development purposes but now I'd like to use Java 7 again.
How do I do this?
It seems to be insanely difficult.
Many thanks in advance.
(I've tried using guigarage and that doesn't work)
From a terminal: export JAVA_HOME=`/usr/libexec/java_home -v 1.x`, where x is the Java version.
I personally have a shell function that does that for me:
use-java () {
export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`
}
I just have to call use-java 7 or use-java 8 in order to change my current shell's Java version.
Use jEnv.
If your system runs homebrew, you can install it using
brew install jenv
(You may need to run brew update to get the latest recipes first)
Add it to your bash profile using
echo 'eval "$(jenv init -)"' >> ~/.bash_profile
Start a new shell to make this change to the profile effective.
You can then add jdk’s like this:
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
list the available versions using
jenv versions
And switch between environments using
jenv global oracle64-1.8.0.25
There’s plenty more custom options, like switching per directory or temporarily in a single shell, see http://www.jenv.be for those.
jEnv works by creating shim scripts for the java executables and putting them in the front of the path. Some 3rd party java tools like ant and maven rely on JAVA_HOME. To make sure JAVA_HOME gets set properly, run
jenv enable-plugin export
There's also jenv plugins for tools like maven and groovy.
Here is an excellent answer for how to switch Java version from the command line in OSX Mavericks (source by Neeme Praks):
Edit your ~/.bash_profile and add the following:
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
echo JAVA_HOME set to $JAVA_HOME
java -version
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
(add above function to your .bash_profile)
Usage:
$ setjdk 1.7
After installation, open the Java Preferences (Launchapad/Others):
and drag the preferred version on top of list:

In Linux How to check through scripts what version of Java is installed?

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).

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.
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.

Categories

Resources