您的位置:首页 > 运维架构 > Tomcat

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

2017-09-30 23:01 671 查看

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: xychenhonglin@163.com* @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 stubStartTomcat 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"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐