How to start/stop Tomcat server in Java

How to start/stop Tomcat server in 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, 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:

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

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!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • How to control Tomcat startup and shutdown in Asp.net
  • Solve the problem that shutting down Tomcat using shutdown.bat will shut down other Tomcats
  • Java code closes the tomcat program and analyzes the problems
  • A bug fix for Tomcat's automatic shutdown

<<:  Summary of MySQL stored procedure permission issues

>>:  Solution to the error message "java.sql.SQLException: Incorrect string value:'\xF0\x9F\x92\xA9\x0D\x0A...'" when storing emoticons in MySQL

Recommend

Detailed process of deploying Docker to WSL2 in IDEA

The local environment is Windows 10 + WSL2 (Ubunt...

Design theory: people-oriented design concept

<br />When thoughts were divided into East a...

Mysql solves the database N+1 query problem

Introduction In orm frameworks, such as hibernate...

Draw a heart with CSS3

Achieve resultsRequirements/Functionality: How to...

CocosCreator general framework design resource management

Table of contents Problems with resource manageme...

Installation and configuration method of vue-route routing management

introduce Vue Router is the official routing mana...

How to Delete Junk Files in Linux Elegantly

I wonder if you are like me, a programmer who arr...

Native js imitates mobile phone pull-down refresh

This article shares the specific code of js imita...

Detailed instructions for installing mysql5.7 database under centos7.2

The mysql on the server is installed with version...

JavaScript to implement the back to top button

This article shares the specific code for JavaScr...

Docker sets up port mapping, but cannot access the solution

#docker ps check, all ports are mapped CONTAINER ...

Implementation of dynamic particle background plugin for Vue login page

Table of contents The dynamic particle effects ar...