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?
source to share
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
source to share
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
source to share