Start a Java Jar Archive with a Script in Linux, BSD and other Unix-like Systems

A Jar Archive is a package of files with the extension .jar, typically Java class files, metadata and resources, packaged on one file to simplify the distribution.
Some of these jar files are executable.

Run a Jar Archive

On almost all systems you can change into the directory of your jar file and simply type:

java -jar XXXX.jar

This even works on Windows like on Unix-like systems when a Java Runtime Envíronment (JRE) is installed.

But if you start the archive often and you're bored, looking for the right directory and typing correctly, you might want to create a script file that performs all the work (for example by double-clicking).

And often the jar archive requires a minimum version of the Java Runtime Environment. You want to show an error message rather than just crashing the application when the JRE is too old.

You can find several scripts while browsing the web, but all I've seen will fail at least when the JRE version switches to 1.10 or to 2.x.

Script Compatibility

The problem is: A shell must execute the script and there is more than one shell. The Bash (Bourne-Again shell) is perhaps the most prominent shell, but bash scripts do normally not work on Debian system that uses the Dash(Debian Almquist shell) instead of the Bash and they will normally not work on BSD, OpenSolaris or other Unix-like systems.

The script presented here was tested on Debian (Dash) and Bash using Linux systems (Manjaro, Mageia) and on FreeBSD/TrueOS. There is a good chance that it will work on other systems too.

Script to run an executable Jar File

You have to set the name of the program, PEA="NAME_OF_THE_PROGRAM" the jar archive to start JAR="YOUR_ARCHIVE.jar", optionally the path to your jar archive PATH_OF_JAR="" and of course the minimum version:

Set 1 for REQUIRED_FIRST_VERSION_NUMBER=1 as long as there is no version 2.
Set the required version number in REQUIRED_SECOND_VERSION_NUMBER=6

The script first checks the default version if it exists and is sufficient. If not, it tries to locate another java version on your system. If no appropriate version is found, an error message is displayed:

Cant find suitable java version. If you want to use this program, you need to install a new Java Runtime Environment.

The Shell Script

	
#!/bin/sh

# Unix script to start an executable jar archive with a minimum version
# requirement on a Unix machine.  
#
# parts #!/bin/shof script are from  http://www.gimlisys.com/articles-detect-java.html
# and  https://stackoverflow.com/questions/7334754/correct-way-to-check-java-version-from-bash-script  
#
# This script should work for bash, dash (as used in debian) and Bourne shell-compatible sh (like in FreeBSD).
# This script should also work for future Java versions, e.g. 1.10 or 2.x
# 
# If you found a bug or an improvement, please send a mail to
# info
#      @
#        eck.cologne
#
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#>>>    YOU CAN/MUST SET THE FOLLOWING FIVE VARIABLES:                                        >>>
#>>>    PEA, JAR, PATH_TO_JAR, REQUIRED_FIRST_VERSION_NUMBER, REQUIRED_SECOND_VERSION_NUMBER  >>>
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# 1. the name of the application
#    this will be displayed before starting
PEA="XXXXXXXXXXXX"

# 2. the executable jar archive to start (you must type the extension .jar)
JAR="XXXXXXXX.jar"

# 3. the path of the jar archive
#    If this file is in the same directory as the executable jar archive, 
#    then this variable should remain empty
PATH_OF_JAR=""

# 4. and 5. Set the required version as two numbers: 
#    only the first two numbers (separated by dots) are taken into account
#    e.g. for 1.8.1_11 set
#    REQUIRED_FIRST_VERSION_NUMBER to 1 and REQUIRED_SECOND_VERSION_NUMBER to 8
REQUIRED_FIRST_VERSION_NUMBER=1
REQUIRED_SECOND_VERSION_NUMBER=8


######################################################################################################

# the java version to use (do not set this vairable)
_JAVA=

# Function to check the version. If the version is suitable,
# this function executes the PEA and exits this script. 
# The variable _JAVA must be set before
check_version_and_run(){

	# to test future version: 
	#REQUIRED_FIRST_VERSION_NUMBER=2
	# to test future version: 
	#REQUIRED_SECOND_VERSION_NUMBER=11

	# get the version number
    	version=$("$_JAVA" -version 2>&1 | awk -F '"' '/version/ {print $2}')
    	# echo using java version is "$version"

	# get the first number of the version:
	# bash only: FIRST_VERSION_NUMBER="$( cut -d '.' -f 1 <<< "$version" )"; 
	FIRST_VERSION_NUMBER="$(echo "$version" | cut -d '.' -f 1)"; 
	# echo first "$FIRST_VERSION_NUMBER"
	
	# check if value is number
	case $FIRST_VERSION_NUMBER in
		''|*[!0-9]*) 
			# set variable to null and return
			_JAVA=
			return ;;
		*)  ;;
	esac

	# get the second number of the version
	SECOND_VERSION_NUMBER="$(echo "$version" |  cut -d '.' -f 2- )"; 
	SECOND_VERSION_NUMBER="$(echo "$SECOND_VERSION_NUMBER" |  cut -d '.' -f 1 )";
	#  echo second "$SECOND_VERSION_NUMBER"
	
	# check if value is number
	case $SECOND_VERSION_NUMBER in
		''|*[!0-9]*) 
			# set variable to null and return
			_JAVA=
			return ;;
		*)  ;;
	esac

	# check first number: continue if succeeds, otherwise reset _JAVA and return
	if  [  "$FIRST_VERSION_NUMBER" -ge "$REQUIRED_FIRST_VERSION_NUMBER" ]; 
	#if  $FIRST_VERSION_NUMBER -ge $REQUIRED_FIRST_VERSION_NUMBER; 
		then
			:
 		else         
			# set variable to null and return
			_JAVA=
			return
	fi

	# check second number: run the PEA and exit script if succeeds, 
	# otherwise reset _JAVA and return
	if [ "$SECOND_VERSION_NUMBER" -ge "$REQUIRED_SECOND_VERSION_NUMBER" ]; 
    		then
             # check if path is empty
             # if not, change directory
             if [ $PATH_OF_JAR ]
	             then
                   cd $PATH_OF_JAR
             fi
			    # start the PEA:
			    echo 'started JRE: ' 
			    ($_JAVA -version)
			    echo Start "$PEA"
			    $_JAVA -jar $JAR
			    exit
    		else              
			    # set variable to null and return
			    _JAVA=
			    return
    	fi
}

######################################################################
# 1. Check default Java to see if it exists and if version is adequate
# alternative:  command -v java >/dev/null 2>&1 || { echo >&2 "java is not installed..."; }

# check if java command is registered:
if command -v java 2>/dev/null; 
	then
		_JAVA="java"
		# check version and execute if required
		check_version_and_run
		# this is only executed if version check failed
		echo 'Your current default version of java is too old. Try to find more recent version...'
	else
		echo 'java is not registered. Try to find a java version...'
fi

######################################################################
# 2. Check JAVA_HOME environment variable (Linux...)
if  $JAVA_HOME
	then
		_JAVA=$JAVA_HOME/bin/java
		check_version_and_run
		# this is only executed if version check failed
		echo 'Your JAVA_HOME environment version is too old. Try to find more recent version...'
	else
		echo 'JAVA_HOME not set. Try to find suitable java version...'
fi

######################################################################
# 3. Use 'locate' to search for other possible java candidates and
#    check their versions.

for JAVA_EXE in $( locate bin/java | grep java$ | xargs echo )
	do
		_JAVA=$(echo $JAVA_EXE)
		#_JAVA=`echo $JAVA_EXE`
		check_version_and_run
		# this is only executed if version check failed
		echo 'Located java version failed. Search continues...'
	done

######################################################################
# Give error message
echo 'Cant find suitable java version.' 
echo 'If you want to use this program, you need to install a new Java Runtime Environment.'
# keep the shell:
$SHELL
				
Suggestions for improvement are welcome. Please send a mail to
info
@
eck.cologne