By Sorawit Laosinchai [ Friday, January 15th, 2010 ]
Every JAVA Developer should know how to set Java runtime options. Tomcat’s document said that setting Tomcat’s Java runtime options is as easy as setting ordinary Java runtime options by set those options in environment variable named JAVA_OPTS. When I try to set multi Java runtime options in Tomcat core script, catalina.bat, it appear that I cannot start my Tomcat Server due to fault configuration in JAVA_OPTS that lead to fault execute command in the last part of core script. What is happening here?
I have to mention about setting multiple Java runtime options. We know each option is separate by space
java -Xms128M -Xmx512M
The problem is having a space in JAVA_OPTS leading to having a space in execute command in the last part of core script.
If you use normal setting, Tomcat will execute by this line.
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
If you trace back to find the real command of this line you will see that the %_EXECJAVA% is set by this statements
if not "%OS%" == "Windows_NT" goto noTitle set _EXECJAVA=start "Tomcat" %_RUNJAVA% goto gotTitle :noTitle set _EXECJAVA=start %_RUNJAVA%
Thats mean the real command that execute in execution line is start with a lot of arguments. So the %JAVA_OPTS% is only an argument of start command. This explained why setting value of multiple Java runtime options in %JAVA_OPTS% as normal multiple Java runtime options when you call java is not working here. Because a space in normal multiple Java runtime options will treat as argument separator of start command and your second Java runtime options will treat as an argument next to %JAVA_OPTS%. Now we know what is the problem and everyone know how to fix the problem ?Just add double quote around value of %JAVA_OPTS%? but is it enough?.
While our propose now is printing double quote around value of %JAVA_OPTS% in execution command but the double quote is a wrapper sign of batch script. So the line
set JAVA_OPTS="-Xms128M -Xmx512M"
without double quote around it in the %JAVA_OPTS% part of start command. Yes, have or have not double quote around the value of set JAVA_OPTS= is the same.
You have two easy options to force printing double quote around value of %JAVA_OPTS% in execution command.
add double quote around %JAVA_OPTS% in each execution line. The fixed version of the above execution line will look like this
%_EXECJAVA% "%JAVA_OPTS%" %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
But don’t forget that it has 4 execution line in Tomcat’s core script. Luckily they are at the last part of the script.
The other way
is adding double quote around double quoted value of set JAVA_OPTS=. The fixed version of set JAVA_OPTS= line will look like this
set JAVA_OPTS=""-Xms128M -Xmx512M""
Yes, its look funny but its work because double quote mean using everything inside double quote as is. As a result, the value of %JAVA_OPTS% is “-Xms128M -Xmx512M” with double quote around it and it will print those double quote in to execution line.
I prefer to latter way because we do not edit any Tomcat’s core script. We only fix our added line
set JAVA_OPTS=""-Xms128M -Xmx512M""
in order to make our application usable.