Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of opening and shutting down tomcat Server by Java

Shulou Source: shulou.com Published: 2022-06-03 15:21:16 09月21日 Update

This article mainly introduces "the method of Java turning on and off tomcat server". In the daily operation, I believe that many people have doubts about the method of opening and closing tomcat server by Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to turn on and off tomcat server by Java". Next, please follow the editor to study!

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"; / / launch the tomcat command / / String command = "E:\\ apache-tomcat-7.0.76\\ bin\\ tomcat7w.exe" / / start the Tomcat command, windows version only, no pop-up box String command = "E:\\ apache-tomcat-7.0.76\\ bin\\ shutdown.bat"; / / close the tomcat command CallTomcat callTomcat = new CallTomcat (); try {callTomcat.callCommand (command);} catch (IOException e) {System.out.println ("error executing command:" + e.getMessage ()) }} / * execute the command * * @ throws IOException * / private void callCommand (String command) throws IOException {Runtime runtime = Runtime.getRuntime (); / / return the runtime object related to the current Java application / / instruct the Java virtual machine to create a child process to execute the specified executable and return the Process object instance Process process = runtime.exec (command) corresponding to the child process; runtime.gc () / / run 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 open command

3.2 execute the close command

4. Common mistakes

It may occur after the code is executed:

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

At this point, you need to set the environment variable:

After I set it up, I still report an error, but log off the computer and run it again.

5. Description

If you execute startup.bat, a pop-up box will appear automatically, but if you execute tomcat7w.exe (depending on the tomcat version), no pop-up box will appear. -- only the windows version, other versions do not have .exe files.

At this point, the study on "how to turn on and off the tomcat server by Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Commands methods servers services versions learning running objects more processes help different practical next size code again variables garbage instances Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Docker Microsoft Xiaomi