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, only for Windows version, no pop-up boxString command = "E:\\apache-tomcat-7.0.76\\bin\\shutdown.bat";//Shutdown tomcat command CallTomcat 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 process = runtime.exec(command); runtime.gc(); //Run the garbage collector String line = null; String content = ""; BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); while((line = br.readLine()) != null) { content += line + "\r\n"; } System.out.println(content); } } 3. Effect preview 3.1 Execute the start command 3.2 Execute shutdown command 4. Common Mistakes After executing the code, the following may occur:
At this time, you need to set the environment variables: After I set it up, it still reported an error when running, but I logged out of the computer and ran it again. 5. Description If you execute startup.bat, a pop-up box will automatically appear, but if you execute tomcat7w.exe (depending on the tomcat version), no pop-up box will appear. --Only Windows version, other versions have no .exe file. Summarize The above is the method of starting/closing the tomcat server in Java introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: Summary of MySQL stored procedure permission issues
The local environment is Windows 10 + WSL2 (Ubunt...
How to add a loading animation every time I scrol...
<br />When thoughts were divided into East a...
Introduction In orm frameworks, such as hibernate...
Achieve resultsRequirements/Functionality: How to...
Preface What is data type conversion? The default...
Table of contents Problems with resource manageme...
introduce Vue Router is the official routing mana...
1. Cause: I need to import a sql file, but I can&...
I wonder if you are like me, a programmer who arr...
This article shares the specific code of js imita...
The mysql on the server is installed with version...
This article shares the specific code for JavaScr...
#docker ps check, all ports are mapped CONTAINER ...
Table of contents The dynamic particle effects ar...