The method of turning on / off tomcat Server by Java
1. Project structure
2.CallTomcat.java
package com.calltomcat.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CallTomcat { public static void main (String[] args) { //String command = "E:\\apache-tomcat-7.0.76\\bin\\startup.bat";//Start tomcat command//String command = "E:\\apache-tomcat-7.0.76\bin\\tomcat7w.exe";/Start Tomcat command, windows version only, no pop-up box String command = "E:\\apache-tomcat-7.0.76\bin\\shutdown.bat";//Close tomcat command CallTomcat = new CallTomcat(); try { callTomcat.callCommand(command);} catch (IOException e) { System.out.println("Error executing command: " + e.getMessage()); } } /** * Execute command * * @throws IOException */ private void callCommand (String command) throws IOException { Runtime runtime = Runtime.getRuntime();//Returns the runtime object associated with the current Java application//instructs the Java virtual machine to create a child process to execute the specified executable program and returns the Process object instance corresponding to the child process Process = runtime.exec(command); runtime.gc();//run garbage collector String line = null; String content = ""; Buffered Reader br = new Buffered Reader (new InputStreamReader(process.getInputStream())); while((line = br.readLine()) != null) { content += line + "\r\n"; } System.out.println(content); }}
3. effect preview
3.1 Execute open command
3.2 Execute shutdown command
4. common mistakes
Executing the code may result in:
The CATALINA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
You need to set environment variables:
I set up after the operation is still error, but the computer will log off to run again.
5. description
A pop-up box appears automatically if you execute startup.bat, but not if you execute tomcat7w.exe(which varies depending on the tomcat version). -- Windows version only, other versions do not have.exe files.
summary
The above is the Java open/close tomcat server method introduced by Xiaobian to you, I hope to help you, if you have any questions, please give me a message, Xiaobian will reply to you in time. Thank you very much for your support!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!