Skip to content

Instantly share code, notes, and snippets.

@kherge
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kherge/624c1289da98ff324053 to your computer and use it in GitHub Desktop.
Save kherge/624c1289da98ff324053 to your computer and use it in GitHub Desktop.
Manages different versions of Java in BASH.

Java Version Manager for BASH

A simple script for managing different versions of Java.

Installation

  1. Create the directory .jvm in your home directory.
  2. Copy jvm to .jvm.
  3. Add . ~/.jvm/jvm to your .bashrc script.
  4. Exit and open a new terminal window.

How It Works

  • You are expected to download the desired JDK or JRE from Oracle.
  • You can put the JDK or JRE wherever you want.
  • You are simply adding paths that will be managed by jvm.
  • You can set a default, which will be automatically made available.
  • You can change the active version of Java, unique to each terminal session.

To learn how to add and remove these directories, run jvm anywhere in your shell.

#!/usr/bin/env bash
# global settings
JVM_HOME="$HOME/.jvm"
JVM_VERSIONS="$JVM_HOME/versions"
JVM_DEFAULT="$JVM_VERSIONS/default"
###
# Manages relaying commands to other JVM functions.
#
# @param string COMMAND The command to execute.
#
# @return integer Returns the status of the last command.
##
function jvm()
{
# make sure home exists
if [ ! -d "$JVM_HOME" ]; then
if ! mkdir "$JVM_HOME"; then
return 1
fi
fi
# make sure versions exists
if [ ! -d "$JVM_VERSIONS" ]; then
if ! mkdir "$JVM_VERSIONS"; then
return 1
fi
fi
# get command name
local COMMAND="$1"; shift
# execute command
case "$COMMAND" in
add)
jvm_add "$@"
;;
default)
jvm_default "$@"
;;
list)
jvm_list
;;
remove)
jvm_remove "$@"
;;
use)
jvm_use "$@"
;;
*)
jvm_usage
;;
esac
return $?
}
###
# Adds a version to manage.
#
# @param string VERSION The JDK or JRE version.
# @param string JAVA_HOME The path to the JDK or JRE.
#
# @return integer The exit status.
##
function jvm_add()
{
local VERSION="$1"
local JAVA_HOME="$2"
# check usage
if [ '' = "$VERSION" ] || [ '' = "$JAVA_HOME" ]; then
cat <<USAGE >&2
Usage: jvm add VERSION JAVA_HOME
Adds a JDK or JRE to manage.
VERSION
The version of the JDK or JRE.
JAVA_HOME
The path to the JDK or JRE.
USAGE
return 0
fi
# check for reserved version
if [ 'default' = "$VERSION" ]; then
echo 'The version "default" is reserved.' >&2
return 1
fi
# check JAVA_HOME
if [ ! -d "$JAVA_HOME" ]; then
echo "$JAVA_HOME: No such directory" >&2
return 1
fi
if [ ! -f "$JAVA_HOME/bin/java" ]; then
echo "$JAVA_HOME: Does not appear to be JAVA_HOME" >&2
return 1
fi
# check existing link
local NEW="$JVM_VERSIONS/$VERSION"
if [ -e "$NEW" ]; then
if ! rm "$NEW"; then
return 1
fi
fi
# create link
if ! ln -s "$JAVA_HOME" "$NEW"; then
return 1
fi
}
###
# Cleans up an initialized environment.
##
function jvm_clean()
{
if [ -e "$JVM_ACTIVE" ]; then
rm "$JVM_ACTIVE"
fi
}
###
# Sets or displays the deafult JDK or JRE.
#
# @param string VERSION The JDK or JRE version to make default.
#
# @return integer The exit status.
##
function jvm_default()
{
local VERSION="$1"
# display version
if [ '' = "$VERSION" ]; then
# check first
if [ -e "$JVM_DEFAULT" ]; then
basename $(readlink "$JVM_DEFAULT")
fi
# set version
else
# check for reserved version
if [ 'default' = "$VERSION" ]; then
echo 'The version "default" is reserved.' >&2
return 1
fi
# check for existing version
local JVM_VERSION="$JVM_VERSIONS/$VERSION"
if [ ! -e "$JVM_VERSION" ]; then
echo "$VERSION: Version not managed" >&2
return 1
fi
# remove existing link
if [ -e "$JVM_DEFAULT" ]; then
if ! rm "$JVM_DEFAULT"; then
return 1
fi
fi
# set new link
if ! ln -s "$JVM_VERSION" "$JVM_DEFAULT"; then
return 1
fi
fi
}
###
# Initializes the current environment.
##
function jvm_init()
{
local FILE=$(tempfile)
if rm "$FILE"; then
echo "$FILE"
fi
}
###
# Lists the manages JDK and JREs.
##
function jvm_list()
{
ls -1 "$JVM_VERSIONS" | grep -Pv '^default$'
}
###
# Removes a version from management.
#
# @param string VERSION The version
##
function jvm_remove()
{
local VERSION="$1"
# check usage
if [ '' = "$VERSION" ]; then
cat <<USAGE >&2
Usage: jvm remove VERSION
Removes a managed JDK or JRE.
VERSION
The version to remove from management.
USAGE
return 0
fi
# check for reserved version
if [ 'default' = "$VERSION" ]; then
echo 'The version "default" is reserved.' >&2
return 1
fi
# remove version
local OLD="$JVM_VERSIONS/$VERSION"
if [ -e "$OLD" ]; then
if ! rm "$OLD"; then
return 1
fi
fi
}
###
# Displays main usage message.
##
function jvm_usage()
{
cat <<USAGE >&2
Usage: jvm COMMAND
Manages different versions of JDKs and JREs.
COMMAND
The command to execute:
add Adds a JDK or JRE to manage.
default Sets or displays the deafult JDK or JRE.
list Lists the managed JDKs and JREs.
remove Removes a managed JDK or JRE.
use Uses a managed JDK or JRE.
USAGE
}
###
# Enables the use of a JDK or JRE.
##
function jvm_use()
{
local VERSION="$1"
# check usage
if [ '' = "$VERSION" ]; then
cat <<USAGE >&2
Usage: jvm remove VERSION
Removes a managed JDK or JRE.
VERSION
The version to remove from management.
USAGE
return 0
fi
# check for reserved version
if [ 'default' = "$VERSION" ]; then
echo 'The version "default" is reserved.' >&2
return 1
fi
# check version
local JVM_VERSION="$JVM_VERSIONS/$VERSION"
if [ ! -e "$JVM_VERSION" ]; then
echo "$VERSION: Version not managed" >&2
return 1
fi
# check existing link
if [ -e "$JVM_ACTIVE" ]; then
if ! rm "$JVM_ACTIVE"; then
return 1
fi
fi
# create new link
if ! ln -s "$JVM_VERSION" "$JVM_ACTIVE"; then
return 1
fi
# set java home
export JAVA_HOME="$JVM_VERSION"
}
# create directory placeholder
if JVM_ACTIVE=$(jvm_init); then
# clean up on exit
trap jvm_clean EXIT
# add to path
export PATH="$JVM_ACTIVE/bin:$PATH"
# activate default
if [ -e "$JVM_DEFAULT" ]; then
jvm_use "$(basename "$(readlink "$JVM_DEFAULT")")"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment