Check if the -XX Java option is supported

We are using various older Java 1.6 versions at work that cannot be updated for various reasons beyond my control.

Some support is XX: ReduceInitialCardMarks , some don't, and exit if you try to use an unsupported option.

I need a wrapper script that calls java with the -XX: ReduceInitialCardMarks option if available, but without it otherwise.

Things I have tried / considered

  • -X, this only prints -X non -XX arguments
  • Version testing? How? You need to know in advance which versions support what
  • Call using a dummy application and checking the return value, eg. java -XX: ReduceInitialCardMarks -jar dummy.jar. There are no suitable classes in rt.jar with static void main () to use, so you need to make your own.

The best approach so far, grepping the output for the Unrecognized VM parameter when called with -version, note -version should be after the argument, since it doesn't test it otherwise.

if java -XX:-ReduceInitialCardMarks -version 2>&1 | grep -q 'Unrecognized VM option'; then echo "Not supported"; else echo "Supported"; fi

      

Is there any clean way to do this?

+3


source to share


2 answers


My machine runs

java -XX:+ReduceInitialCardMarks -version

      

returns 0 and version information (since this option is supported) and execute



java -XX:+NoSuchOption -version

      

returns 1 with some error messages on stderr. If this is true for your Java versions, you can simply do

if java -XX:+ReduceInitialCardMarks -version; then
    echo "Supported"
else
    echo "Unsupported"
fi

      

+2


source


You can try something like:

if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then   
    _java="$JAVA_HOME/bin/java"

if [[ "$_java" ]]; then
    version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
    echo version "$version"
    if [[ "$version" > "1.6" ]]; then
        # do work
    else         
        echo version is less than 1.6
        exit
    fi
fi

      



See: Correct way to check Java version with BASH script

0


source







All Articles