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

Analysis of the problem of deploying vue project and configuring proxy in Nginx

1. Install and start nginx # Install nginx sudo a...

Basic application methods of javascript embedded and external links

Table of contents Basic application of javascript...

The process of installing Docker in Linux system

In this blog, I will walk you through the process...

How to detect Ubuntu version using command line

Method 1: Use the lsb_release utility The lsb_rel...

XHTML 1.0 Reference

Arrange by functionNN : Indicates which earlier ve...

How to implement the observer pattern in JavaScript

Table of contents Overview Application scenarios ...

Vue implements three-dimensional column chart based on echarts

The three-dimensional column chart consists of th...

Introduction to setting up Tomcat to start automatically on Linux system

1. Enter the /etc/init.d directory: cd /etc/init....

Summary of MySQL development standards and usage skills

1. Naming conventions 1. Database names, table na...

Teach you how to subcontract uniapp and mini-programs (pictures and text)

Table of contents 1. Mini Program Subcontracting ...

Two implementation codes of Vue-router programmatic navigation

Two ways to navigate the page Declarative navigat...

How to add Nginx to system services in CentOS7

Introduction After compiling, installing and solv...

iframe src assignment problem (server side)

I encountered this problem today. I reassigned the...