[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Proposed addition to java-common



Package: java-common
Severity: wishlist

Hi.  Policy states that Java programs must run without specific environment 
variables, which can be some hassle when writing a program that works with 
several JVMs.  Different JVMs use different bootstrap classes - which 
sometimes need to be passed on the command line (eg. kaffe) - and often even 
have different binaries for execution (/usr/bin/java, /usr/bin/kaffe, etc).

I have written a script that will try to guess which command to use for the 
java runtime and which bootstrap classes need to be passed on the command 
line.  It does this by trying some standards commands (java, kaffe, etc).  
When it finds one that works, it runs it to get version information; from 
that it determines which JVM is being used and from there works out what 
bootstrap classes to pass.  All decisions it makes can be manually overridden 
by setting the appropriate environment variables (although the entire point 
is that you don't *need* to set such variables and everything should still 
work).

I've only tested blackdown and kaffe (japhar won't even run on my system) but 
it should be relatively straightforward to add other JVMs to the recognition 
list.

My proposal is that this script be included in java-common, since in theory 
it could be used to create a runtime script for almost every java program 
that is packaged.  I currently have it on my system as /usr/bin/find_java.

If this is accepted for java-common, I will happily write a full man page to 
go with it.  There is also a /usr/bin/find_javac in the works with the 
obvious alternate goal.

The script /usr/bin/find_java is included below.

Ben.


#!/bin/sh
set -e

# find_java - Script utilities for guessing Java runtime and classpath
#
# Copyright (c) 2001 Ben Burton <benb@acm.org>
# Written for Debian GNU/Linux
# Released under LGPL
#
# Usage: find_java [ --eval | --print ]
#
# --eval:
#     Writes a shell command to standard output that can be slurped into
#     another script using eval.  The output will look like:
#
#         JVM="funky" ;
#         JAVA="chickens" ;
#         BOOTSTRAP_CLASSES="dance"
#
#     The following line in your own script will thus call this script
#     and set the corresponding variables in your script:
#
#         eval `find_java --eval`
#
# --print:
#     Writes its guesses to standard output in human-readable form.
#
# $JAVA will be set to the command that starts the Java interpreter
# (such as /usr/bin/java).
#
# $JVM will be set to a token describing which particular Java virtual machine
# is called by $JAVA.  Tokens that are currently returned are listed
# below.
#
# $BOOTSTRAP_CLASSES will be set to the part of the classpath
# containing the core Java classes.  This will be left empty if $JAVA
# does not require this part of the classpath to be specified.
#
# If any of these variables are already set in the environment, the
# values from the environment will be used.
#
# This script is not guaranteed to work; it merely makes educated
# guesses.  It should work for all the JVMs distributed with Debian.
#
# Exit status: 0 on success, 1 if no intelligent guesses could be made.
#
# JVM tokens:
#     blackdown: Blackdown port of the Sun Java runtime
#     kaffe: Kaffe virtual machine from Transvirtual
#     unknown: Some other JVM

case "$1" in
	--eval )
	;;
	--print )
	;;
	* )
		echo "Usage: $0 [ --eval | --print ]"
		echo "See comments at the beginning of this script for further details."
		exit 1
	;;
esac

# Determine the java runtime command.
if [ -z "$JAVA" ]; then
	# Check for common JVMs.
	if which java >& /dev/null; then
		JAVA=`which java`
	elif which kaffe >& /dev/null; then
		JAVA=`which kaffe`
	else
		echo "Cannot locate a Java virtual machine."
		echo
		echo "Set the environment variable \$JAVA to the command that starts"
		echo "your Java interpreter (such as /usr/bin/java) and try again."
		echo
		exit 1
	fi
	export JAVA
fi

# Determine whose JVM we are using.
if ! version=`"$JAVA" -version 2>&1`; then
	version=
fi
case "$version" in
	# Search for a string not included in $JAVA so we don't accidentally
	# catch output such as "kaffe: Command not found" and the like.
	*[Tt]ransvirtual* )
		JVM=kaffe
	;;
	*[Bb]lackdown* )
		JVM=blackdown
	;;
	* )
		# No idea.
		JVM=unknown
	;;
esac

# Determine the bootstrap classes.
if [ -z "$BOOTSTRAP_CLASSES" ]; then
	case "$JVM" in
		kaffe )
			if [ -e /usr/share/kaffe/Klasses.jar ]; then
				kaffejars=`ls /usr/share/kaffe/*.jar`
				BOOTSTRAP_CLASSES=`echo $kaffejars | tr ' ' ':'`
			else
				echo "The core Kaffe classes could not be found."
				echo
				echo "Set the environment variable \$BOOTSTRAP_CLASSES to a"
				echo "colon-separated list of jars containing the core Kaffe"
				echo "classes (such as /usr/share/kaffe/Klasses.jar) and try again."
				echo
				exit 1
			fi
		;;
		blackdown )
			# Don't need to specify bootstrap classes.
			BOOTSTRAP_CLASSES=
		;;
		* )
			# Let's hope we have a JVM that automagically knows where its
			# bootstrap classes are.
			BOOTSTRAP_CLASSES=
		;;
	esac
	export BOOTSTRAP_CLASSES
fi

case "$1" in
	--eval )
		echo "JVM=\"$JVM\" ;"
		echo "JAVA=\"$JAVA\" ;"
		echo "BOOTSTRAP_CLASSES=\"$BOOTSTRAP_CLASSES\""
	;;
	--print )
		echo "JVM: $JVM"
		echo "Runtime: $JAVA"
		echo "Bootstrap classes: $BOOTSTRAP_CLASSES"
	;;
esac




Reply to: