diff --git a/libexec/jenv-with b/libexec/jenv-with new file mode 100755 index 0000000..e5a3bf6 --- /dev/null +++ b/libexec/jenv-with @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# +# Summary: Execute command with specific Java version +# +# Usage: jenv with +# +# Executes the command with specified Java version being effective. +# This is equivalent to running `jenv shell ' followed by +# the command, but without changing the shell environment. +# +# should be a string matching a Java version known to jenv. +# The special version string `system' will use your default system Java. +# Run `jenv versions' for a list of available Java versions. + +set -e +[ -n "$JENV_DEBUG" ] && set -x + +if [ "$#" -lt 2 ]; then + jenv-help --usage with >&2 + exit 1 +fi + +# Provide jenv completions +if [ "$1" = "--complete" ]; then + echo --unset + echo system + exec jenv-versions --bare +fi + +export JENV_VERSION="$1" +shift + +# reset & isolate from outer environment +export JAVA_HOME="" +export JENV_FORCEJAVAHOME=true +export JDK_HOME="" +export JENV_FORCEJDKHOME=true + +# TODO or may `jenv-javahome` should exit with 0 (printing nothing) when JENV_VERSION=system +# then we would not need to special case `system` here +if [ "$JENV_VERSION" != "system" ]; then + JAVA_HOME=$(jenv-javahome) + if [ -n "$JAVA_HOME" ] && [ -e "$JAVA_HOME/bin/javac" ]; then + JDK_HOME="$JAVA_HOME" + fi +fi + +exec "$@"