Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing system properties that contains spaces to Tomcat through JAVA_OPTS

I need to pass multiple system properties to Tomcat 6 through the JAVA_OPTS environment variable. I can't seem to pass system properties that contain spaces:

JRE_HOME=/root/jre1.6.0_34/ JAVA_OPTS="-DsysProp1=foo -DsysProp2=bar with spaces" ./catalina.sh run

Fails with:

Using CATALINA_BASE:   /root/apache-tomcat-6.0.37
Using CATALINA_HOME:   /root/apache-tomcat-6.0.37
Using CATALINA_TMPDIR: /root/apache-tomcat-6.0.37/temp
Using JRE_HOME:        /root/jre1.6.0_34/
Using CLASSPATH:       /root/apache-tomcat-6.0.37/bin/bootstrap.jar
Exception in thread "main" java.lang.NoClassDefFoundError: with
Caused by: java.lang.ClassNotFoundException: with
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: with.  Program will exit.   

I looked around on SO and the answers don't seem to help. Tried all of these with no success:

JRE_HOME=/root/jre1.6.0_34/ JAVA_OPTS="-DsysProp1=foo -DsysProp2=\"bar with spaces\"" ./catalina.sh run

JRE_HOME=/root/jre1.6.0_34/ JAVA_OPTS='-DsysProp1=foo -DsysProp2="bar with spaces"' ./catalina.sh run

JRE_HOME=/root/jre1.6.0_34/ JAVA_OPTS='-DsysProp1=foo -DsysProp2=bar\ with\ spaces' ./catalina.sh run
like image 364
AlexBrand Avatar asked Aug 21 '13 13:08

AlexBrand


1 Answers

As an alternative you could put the property settings into conf/catalina.properties instead of using JAVA_OPTS. This is a standard java.util.Properties format file so you don't need to quote anything, it simply takes everything before the first equals sign, colon or space as the property name and everything after that as the value:

sysProp2=bar with spaces

If you can't (or would prefer not to) modify that file directly, you can copy it to another location, edit the copy, and then pass

-Dcatalina.config=file:/path/to/copy/of/catalina.properties

in JAVA_OPTS to make it load your properties from there.

like image 198
Ian Roberts Avatar answered Oct 22 '22 23:10

Ian Roberts