gpt4 book ai didi

java - 从 Java 代码启动和停止 Tomcat

转载 作者:行者123 更新时间:2023-11-28 22:42:56 26 4
gpt4 key购买 nike

基于我在 Stackoverflow 和 Internet 上的其他页面中看到的代码,我创建了一种方法来在我将在我的系统中运行一个进程时停止和启动 tomcat,因为我需要清理我的操作系统中的内存,我使用 System.gc() 但仍然不足以释放内存,这是代码:

全局声明:

private String server = "localhost";

停止启动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();
}
}

启动 tomcat 的代码行完美运行,但没有停止它的说明,因为当我实例化套接字时,会给我以下消息:Connection refused: connect

我该如何解决这个问题?或者,还有其他方法可以停止 tomcat 吗?

提前致谢。

最佳答案

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 = 您的 tomcat bin 文件的路径
例如 - /home/zakaria/Téléchargements/apache-tomcat-8.0.21/bin

关于java - 从 Java 代码启动和停止 Tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21321897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com