3

We're using various older versions of Java 1.6 at work which can't be updated for assorted reasons outside my control.

Some support -XX:ReduceInitialCardMarks, some do not, and exit if you try to use an unsupported option.

I'd like a shell script which calls java with the -XX:ReduceInitialCardMarks option if it is available, but otherwise without.

Things I've tried/considering

  • -X, this only prints out -X arguments not -XX
  • Testing the version? How? Would need upfront knowledge of which versions support what
  • Calling with a dummy application and testing return value, e.g. java -XX:ReduceInitialCardMarks -jar dummy.jar. No suitable classes in rt.jar with static void main() to use, so have to make my own.

Best approach so far, grepping the output for Unrecognized VM option when calling with -version, note -version has to be after the argument as otherwise it doesn't test it.

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 of doing this?

Adam
  • 35,919
  • 9
  • 100
  • 137

2 Answers2

2

On my machine, executing

java -XX:+ReduceInitialCardMarks -version

returns 0 and the version information (because the option is supported), and executing

java -XX:+NoSuchOption -version

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

if java -XX:+ReduceInitialCardMarks -version; then
    echo "Supported"
else
    echo "Unsupported"
fi
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • I like it, best so far, cleaner than my grep approach. I'll hold off accepting for a little while in case anyone else has some ideas. – Adam Sep 12 '14 at 04:55
0

You could 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 from BASH script

Community
  • 1
  • 1
x4nd3r
  • 855
  • 1
  • 7
  • 20
  • Thanks, the versioning is not straightforward, 1.6.0_18 supports this argument but 1.6.0_15 does not etc... – Adam Sep 12 '14 at 04:42