Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of opening and shutting down tomcat Server by Java

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report