使用java代码在windows中启动Tomcat

使用java代码在windows中启动Tomcat,java,Java,我想用java程序启动tomcat 我从linux开始使用以下代码 String command = "c:\program files\tomcat\bin\startup.sh"; Process child = Runtime.getRuntime().exec(command); 它很好用 我在windows中使用了以下代码 String command = "c:/program files/tomcat/bin/startup.bat"; Process child = Runti

我想用java程序启动tomcat

我从linux开始使用以下代码

String command = "c:\program files\tomcat\bin\startup.sh";
Process child = Runtime.getRuntime().exec(command);
它很好用

我在windows中使用了以下代码

String command = "c:/program files/tomcat/bin/startup.bat";
Process child = Runtime.getRuntime().exec(command);
它不起作用

是否要为windows配置任何其他属性


提前感谢。

请尝试正确设置环境变量:

  • 爪哇之家酒店
    • 您的代码是非法的。 在Java中,反斜杠需要使用“\\”。
      然后,设置环境变量JAVA\u HOME和CATALINA\u HOME。

      我也有同样的问题。只需使用

       String command = "D:/server/apache-tomcat-7.0.67/bin/startup.bat";
       File file = new File("D:/server/apache-tomcat-7.0.67/bin");
       Process p = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C", "start" ,command, 
       "start"}, null, file);
       Process process = new ProcessBuilder(command , "start" ).start();
      

      工作正常。但是有一件事文件路径没有任何空格。

      c:\ProgramFiles
      在linux中不工作?有任何异常吗?是的。找不到指定的文件路径
      Process
      对象有
      getErrorStream()
      方法,试着读一下上面写的内容。文件路径是:C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin\startup.bat java.io.IOException:无法运行程序“C:\Program”:CreateProcess error=2,系统找不到指定的文件。你确定不想只嵌入Tomcat并以编程方式启动它吗?只有java\u HOME,不需要设置CATALINA_HOME我尝试设置JAVA_HOME的环境变量,但它不起作用tomcat服务器在linux机器中的位置是什么?您是否使用过:“设置JAVA\u HOME=”然后从同一个终端运行?只有JAVA\u HOME,不需要设置CATALINA\u HOME也不需要设置CATALINA HOME。