-XX:OnOutOfMemoryError=“ ” gives Error : Could not find or load main class

后端 未结 1 1665
自闭症患者
自闭症患者 2021-01-17 01:12

I have set JAVA_OPTS as

export JAVA_OPTS=\"$JAVA_OPTS -server -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:gc.log  -Dcom.sun.management.jmxremote.ssl=f         


        
相关标签:
1条回答
  • 2021-01-17 01:42

    Try

    export JAVA_OPTS="... \"-XX:OnError=$TEMP_CMD\" ..."

    or

    export JAVA_OPTS='... "-XX:OnError=$TEMP_CMD" ...'

    See Bash nested quotes and eval and http://www.grymoire.com/Unix/Quote.html.

    Update

    The above appears still not to work after testing.

    test.sh

    JAVA_OPTS="$JAVA_OPTS -Xmx32m '-XX:OnOutOfMemoryError=echo %p'"
    java $JAVA_OPTS Test
    

    gives

    $ bash -x ./test.sh
    + JAVA_OPTS=' -Xmx32m '\''-XX:OnOutOfMemoryError=echo %p'\'''
    + java -Xmx32m ''\''-XX:OnOutOfMemoryError=echo' '%p'\''' Test
    Exception in thread "main" java.lang.NoClassDefFoundError: '-XX:OnOutOfMemoryError=echo
    

    fail.

    JAVA_OPTS="$JAVA_OPTS -Xmx32m -XX:OnOutOfMemoryError=\"echo %p\""
    java $JAVA_OPTS Test
    

    gives

    $ bash -x ./test.sh
    + JAVA_OPTS=' -Xmx32m -XX:OnOutOfMemoryError="echo %p"'
    + java -Xmx32m '-XX:OnOutOfMemoryError="echo' '%p"' Test
    Exception in thread "main" java.lang.NoClassDefFoundError: %p"
    

    fail.

    Diagnostics with -x bash option helped to google that the root of the problem is in a strange mix of bash variable substitution and word splitting rules: http://mywiki.wooledge.org/BashFAQ/050.

    There are several possible workarounds.

    1) Use helper script to get rid of obstinate spaces

    JAVA_OPTS+=" -Xmx32m -XX:OnOutOfMemoryError=/usr/tmp/test/oom.sh"
    java $JAVA_OPTS Test
    

    2) Move OnOutOfMemoryError out of variable

    JAVA_OPTS="$JAVA_OPTS -Xmx32m"
    java $JAVA_OPTS -XX:OnOutOfMemoryError="echo %p" Test
    

    gives

    $ bash -x ./test.sh
    + JAVA_OPTS=' -Xmx32m'
    + /usr/java/jdk1.6.0_16/bin/java -Xmx32m '-XX:OnOutOfMemoryError=echo %p' Test
    #
    # java.lang.OutOfMemoryError: Java heap space
    # -XX:OnOutOfMemoryError="echo %p"
    #   Executing /bin/sh -c "echo 1639"...
    1639
    
    0 讨论(0)
提交回复
热议问题