Tomcat 通过java程序启动和停止,以及解决The CATALINA_HOME environment variable is not defined correctlyThis environ

package com.example.springboot.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @Author: HONGLINCHEN
* @Description: 程序启动 tomact 和关闭
* @Email: [email protected]
* @Date: 2017-9-30 23:00
*/
public class StartTomcat {
public void close() throws IOException
{
Process process = Runtime. getRuntime().exec( " cmd /c E: \\ apache-tomcat-7.0.79 \\ bin \\ shutdown.bat"); // 调用外部程序
final InputStream in = process.getInputStream();
BufferedReader br= new BufferedReader( new InputStreamReader(in));
StringBuilder buf = new StringBuilder();
String line = null;
while((line = br.readLine()) != null)
buf.append(line);
System. out.println( " 输出结果为: " + buf);
}
public void start() throws IOException
{
Process process = Runtime. getRuntime().exec( " cmd /c E: \\ apache-tomcat-7.0.79 \\ bin \\ startup.bat"); // 调用外部程序
final InputStream in = process.getInputStream();
BufferedReader br= new BufferedReader( new InputStreamReader(in));
StringBuilder buf = new StringBuilder();
String line = null;
while((line = br.readLine()) != null)
buf.append(line);
System. out.println( " 输出结果为: " + buf);
}
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
StartTomcat con= new StartTomcat();
con.start();
//con.close();
}
}
cmd /c  可以不要
如果启动不成功,控制台打印下面的提示,
The CATALINA_HOME environment variable is not defined correctlyThis environment variable is needed to run this program
第一请检查tomcat环境变量的配置
第二打开tomcat bin目录下,找到startup.bat
用motepad++编辑
找到 set "CURRENT_DIR=%cd%"这段  改成 set "CURRENT_DIR=E:\apache-tomcat-7.0.79"
注意 只有一对双引号   %%去掉
同理如果运行shutdown.bat也提示上面的错,那么shutdown.bat文件中 set "CURRENT_DIR=%cd%"这段  也要改成 set "CURRENT_DIR=E:\apache-tomcat-7.0.79"

你可能感兴趣的:(Tomcat 通过java程序启动和停止,以及解决The CATALINA_HOME environment variable is not defined correctlyThis environ)