从Java code启动和停止Tomcat [英] Start and Stop Tomcat from java code

查看:123
本文介绍了从Java code启动和停止Tomcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于在code我和#1 Internet上的其他页面看到

,我创建了一个方法停止,并在瞬间启动tomcat,我会在我的系统运行的过程,因为我需要清理内存我的操作系统,我使用的System.gc(),但仍不足以释放内存,这是code:

全局声明:

 私有String =服务器localhost的;

方法停止 - 启动tomcat:

 公共无效的tomcat(){
    尝试{
        插座S =新的Socket(服务器,8005);
        如果(s.isConnected()){
            PrintWriter的打印=的新PrintWriter(s.getOutputStream(),TRUE);
            print.println(关机); 8005关机=关机>中; / *命令按照行服务器端口=&LT停止Tomcat在CATALINA_HOME / conf目录/ server.xml中* /
            print.close();
            S.CLOSE();
        }
        。调用Runtime.getRuntime()EXEC(System.getProperty(的catalina.home)+\\\\ \\\\斌的startup.bat); / *它被停止后指令来运行Tomcat * /
    }赶上(例外前){
        ex.printStackTrace();
    }
}

在code线启动Tomcat完美的作品,但没有说明停止它,因为当我实例上的插座,使我有以下消息:连接被拒绝:连接

我该如何解决呢?或者,是否有另一种方式来停止Tomcat?

先谢谢了。


解决方案

 公共静态无效shut_server(字符串留置权)
{
      尝试{        。孩子的过程=调用Runtime.getRuntime()EXEC(留置权+/ shutdown.sh);
        的System.out.println(Serveur EST atteins);
    }赶上(IOException异常前){
        。Logger.getLogger(Installation.class.getName())日志(Level.SEVERE,空,前);
        的System.out.println(ERREUR德demarrage);
    }
}

留置权 = 路径到Tomcat bin文件的结果
例如 - /home/zakaria/Téléchargements/apache-tomcat-8.0.21/bin

Based in a code I saw in Stackoverflow and other pages on Internet, I've created a method to stop and start tomcat at the moment I'll run a process in my system because I need to clean memory in my OS, I use System.gc() but still not enough to free memory, this is the code:

Global declaration:

private String server = "localhost";

Method to stop-start tomcat:

public void tomcat(){
    try{
        Socket s = new Socket(server,8005);
        if(s.isConnected()){
            PrintWriter print = new PrintWriter(s.getOutputStream(),true);
            print.println("SHUTDOWN"); /*Command to stop tomcat according to the line "<Server port="8005" shutdown="SHUTDOWN">" in catalina_home/conf/server.xml*/
            print.close();
            s.close();
        }
        Runtime.getRuntime().exec(System.getProperty("catalina.home")+"\\bin\\startup.bat");  /*Instruction to run tomcat after it gets stopped*/
    }catch (Exception ex){
        ex.printStackTrace();
    }
}

The code line to start tomcat works perfectly, but no the instructions to stop it because, when I instance the socket, gives me the following message: Connection refused: connect.

How can I solve this? or, is there another way to stop tomcat?

Thanks in advance.

解决方案

public static void shut_server(String lien) 
{
      try {

        Process child = Runtime.getRuntime().exec(lien+"/shutdown.sh");
        System.out.println("Serveur est atteins");
    } catch (IOException ex) {
        Logger.getLogger(Installation.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("erreur de demarrage");
    }
}

lien = path to your tomcat bin file
for example - /home/zakaria/Téléchargements/apache-tomcat-8.0.21/bin

这篇关于从Java code启动和停止Tomcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆