简体   繁体   中英

Spaces in JAVA_OPTS in Apache Tomcat

How can I pass a property which has spaces using JAVA_OPTS to Apache Tomcat?

For example;

-Dmy.property="How are you"

My operating system is SUSE Linux.

I actually figured this out using AWS Elasticbeanstalk, which lets you have spaces in the Environment Properties you can enter via the UI.

As part of the build of the server instance, the Elasticbeanstalk service replaces the /usr/bin/tomcat7 script in order to accommodate some of its requirements.

If you check this, you can see the following difference:

Default script:

if [ "$1" = "start" ]; then
    ${JAVACMD} $JAVA_OPTS $CATALINA_OPTS \

Elasticbeanstalk script:

if [ "$1" = "start" ]; then
    eval "${JAVACMD} $JAVA_OPTS $CATALINA_OPTS \
    ...."

ie they have placed an "eval" before the command to start the JVM, and enclosed the entire command in double-quotes.

This appears to allow the JAVA_OPTS values with spaces be preserved.

Seems to me there is no way to use spaces in JAVA_OPTS, I have the same problem on OSX. You can add your property directly to other -D options in the catalina.sh

Let's open file $CATALINA_HOME\\bin\\catalina.sh . You will see the guide from Apache Tomcat for variable JAVA_OPTS

#   JAVA_OPTS       (Optional) Java runtime options used when any command
#                   is executed.
#                   Include here and not in CATALINA_OPTS all options, that
#                   should be used by Tomcat and also by the stop process,
#                   the version command etc.
#                   Most options should go into CATALINA_OPTS.

You want set property -Dmy.property="How are you" , it is not a property that JVM supported. You should put it in variable CATALINA_OPTS . If your property's value has space(s), wrap it inside " " .

#   CATALINA_OPTS   (Optional) Java runtime options used when the "start",
#                   "run" or "debug" command is executed.
#                   Include here and not in JAVA_OPTS all options, that should
#                   only be used by Tomcat itself, not by the stop process,
#                   the version command etc.
#                   Examples are heap size, GC logging, JMX ports etc.

(Although you used Apache Tomcat 5.5, I quoted guide from Apache Tomcat 9.0.11 because it is existing in my computer)

尝试这个

-Dmy.property="How\ are\ you"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM