JAVA调用TOMCAT的CATALINA命令常见问题

问题1:

原先java执行bat命令的代码可能是 cmd /c start $TOMCAT_HOME\bin\catalina.bat

执行后出现错误

The CATALINA_HOME environment variable is not defined correctly
This environment variable is needed to run this program

 

原因:

打开startup.bat文件

rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHome

是由于读取不到CATALINA_HOME

 

解决:

d:\tomcat 是tomcat的地址

1。在系统配置中添加$CATALINA_HOME的环境变量(开发使用多个tomcat不推荐)

2。在执行命令之前添加set "CATALINA_HOME=d:\tomcat "

3。执行如下代码,

API中对于最后一个参数的解释为:

dir - 子进程的工作目录;如果子进程应该继承当前进程的工作目录,则该参数为 null

 

注:catalina命令为 start debug run stop

run和start的区别为run在当前命令窗口中执行

 

public static void main(String[] args) throws Exception {
    Process proc = Runtime.getRuntime().exec("cmd /c start d:\\tomcat\\bin\\catalina.bat start", null, new File("d:\\tomcat"));
    proc.waitFor();
    proc.destroy();
}

你可能感兴趣的:(java,tomcat,C++,c,C#)