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
1. Install and start nginx # Install nginx sudo a...
Table of contents Basic application of javascript...
In this blog, I will walk you through the process...
Having used MySQL for such a long time, I believe...
Method 1: Use the lsb_release utility The lsb_rel...
Arrange by functionNN : Indicates which earlier ve...
Table of contents Overview Application scenarios ...
The three-dimensional column chart consists of th...
1. Enter the /etc/init.d directory: cd /etc/init....
1. Naming conventions 1. Database names, table na...
Install MySQL under Windows for your reference. T...
Table of contents 1. Mini Program Subcontracting ...
Two ways to navigate the page Declarative navigat...
Introduction After compiling, installing and solv...
I encountered this problem today. I reassigned the...