停止使用Java代码运行服务器?

停止使用Java代码运行服务器?,java,Java,我想停止在端口8080上运行的服务器。在我的java应用程序中,每当应用程序关闭时,该服务器也需要停止。但除了手动停止服务器,我找不到任何解决方案。有没有办法用代码停止服务器?顺便说一句,我正在使用Windows 7,因为我没有看到下面的代码“如何创建服务器和接受连接?” try { // Execute a command to terminate your server String command = "command to stop your server"; P

我想停止在端口8080上运行的服务器。在我的java应用程序中,每当应用程序关闭时,该服务器也需要停止。但除了手动停止服务器,我找不到任何解决方案。有没有办法用代码停止服务器?顺便说一句,我正在使用Windows 7,因为我没有看到下面的代码“如何创建服务器和接受连接?”
try {
    // Execute a command to terminate your server
    String command = "command to stop your server";
    Process child = Runtime.getRuntime().exec(command);

} catch (IOException e) {
}
我给了你以下建议。你应该试着实现第一种方法, 剩下的选项不能保证是否会终止正确的进程

  • 您需要在Runtime.getRuntime()TASKKILL/F/IM中指定此选项,或者有一个jps更好地终止相关进程

    {


  • 由于我没有看到代码,下面是如何创建服务器和接受连接的 我给了你以下建议。你应该试着实现第一种方法, 剩下的选项不能保证是否会终止正确的进程

  • 您需要在Runtime.getRuntime()TASKKILL/F/IM中指定此选项,或者有一个jps更好地终止相关进程

    {


  • 为什么不检查?它提供了一个Java API来启动/停止Java容器。您可以在主页中找到受支持容器的列表。

    为什么不检查?它提供了一个Java API来启动/停止Java容器。您可以在主页中找到受支持容器的列表。

    您如何启动SymmetricDs?作为windows服务,作为战争还是嵌入到应用程序中

    看看用户指南,如果你能将它嵌入到你的代码中,你应该能够直接启动和停止它

    import org.jumpmind.symmetric.SymmetricWebServer;
    
    public class StartSymmetricEngine {
    
        /**
         * Start an engine that is configured by two properties files. One is
         * packaged with the application and contains overridden properties that are
         * specific to the application. The other is found in the application's
         * working directory. It can be used to setup environment specific
         * properties.
         */
        public static void main(String[] args) throws Exception {
    
            SymmetricWebServer node = new SymmetricWebServer(
                                       "classpath://my-application.properties");
    
            // this will create the database, sync triggers, start jobs running
            node.start(8080);
    
            // this will stop the node
            node.stop();
        }   
    
    }
    

    如何启动SymmetricDs?作为windows服务、WAR或嵌入应用程序

    看看用户指南,如果你能将它嵌入到你的代码中,你应该能够直接启动和停止它

    import org.jumpmind.symmetric.SymmetricWebServer;
    
    public class StartSymmetricEngine {
    
        /**
         * Start an engine that is configured by two properties files. One is
         * packaged with the application and contains overridden properties that are
         * specific to the application. The other is found in the application's
         * working directory. It can be used to setup environment specific
         * properties.
         */
        public static void main(String[] args) throws Exception {
    
            SymmetricWebServer node = new SymmetricWebServer(
                                       "classpath://my-application.properties");
    
            // this will create the database, sync triggers, start jobs running
            node.start(8080);
    
            // this will stop the node
            node.stop();
        }   
    
    }
    


    它是一个Apache服务器,还是一个你自己开发的服务器?如果是这样的话,处理的方式会完全不同。它是一个独立的java应用程序还是一个基于web的应用程序?我想发送一个命令,它将停止在端口8080上运行。这就是我想通过一个命令停止这个本地主机:8080服务器。这个命令将被发送到命令promt服务器看起来像apache服务器。SymmetricDs服务器。此服务器配置数据库配置。此服务器不是我开发的服务器,它是SymmetricDs服务器。它是例如apache服务器,还是您自己开发的服务器?如果是的话,实现方式将完全不同。它是独立的java应用程序还是基于web的?我想知道发送一个命令,该命令将提供在端口8080上停止运行的功能。这就是我想通过命令promt停止这个本地主机:8080服务器。这个服务器看起来像apache服务器。SymmetricDs服务器。这个服务器配置数据库schronization。这个服务器不是我开发的服务器,它是SymmetricDs服务器。那么,需要什么com例如,我在8080端口上运行的服务器是2684@Suresh,我正在使用windows OS
    taskkill/PID 2684
    或更好的命令,例如,您想关闭tomcat,只需从适当的pathWell执行
    shutdown.bat
    ,什么是必需的命令?。例如,我的服务器在端口8080 PID上运行2684@Suresh, 我使用的是windows操作系统
    taskkill/PID 2684
    或更好的命令,例如,您想关闭tomcat,只需从相应的pathexec(“taskkill/PID 2684”)执行
    shutdown.bat
    ,这是真的吗?@yuce我不理解上面的一个.exec(“停止本地主机服务的必要命令:8080”)什么是necasary命令提示符命令?exec(“taskkill/PID 2684”)是真的吗?@yuce我不懂上面的一个。exec(“停止服务的necasary命令localhost:8080”)什么是必需的命令提示符命令?让我告诉你一个问题?如果你有一个像xammp apache服务器一样运行在localhost:8080上的服务器,你如何通过单击一个按钮来停止这个服务器?我的意思是,我只想在我的应用程序关闭时停止这个服务器。也就是说,我想向命令提示符发送一个命令来停止它s 8080端口。首先签入任务管理器,使用服务器正在运行的名称,然后在cmd提示符TASKKILL/F FI“WINDOWTITLE eq*”@Suresh中尝试此命令。我创建了一个包含运行服务器代码的bat文件。然后,我执行此bat文件Runtime rn=Runtime.getRuntime();Process pr=rn.exec(“main/resources/RunPort8080.bat”);此代码没有错误。但当我的程序关闭时,服务器仍在运行。我不知道为什么它仍在运行。当我在命令提示符下运行RunPort8080.bat文件时,然后关闭命令提示符服务器,但服务器未运行。问题是缺少“pr.exit();”声明?@Yuce您看过Richard Miskin的评论吗,@Suresh是的,我看过,我以前看过这些代码。我的问题是一般性的,不包括对称ds服务器,但包括所有windows服务器。让我告诉您一个问题?如果您的服务器运行在localhost:8080上,比如xammp apache服务器,您如何通过单击b来停止此服务器utton?我的意思是,我只想在应用程序关闭时停止此服务器。也就是说,我想向命令提示符发送一个命令,以停止此8080端口。首先在任务管理器中签入服务器正在运行的名称,然后在cmd提示符TASKKILL/F FI“WINDOWTITLE eq*”中尝试此命令@Suresh我创建了一个包含运行服务器代码的bat文件。然后我执行了这个bat文件Runtime rn=Runtime.getRuntime();Process pr=rn.exec(“main/resources/RunPort8080.bat”);此代码没有错误。但当我的程序关闭时,服务器仍在运行。我不知道为什么它仍在运行。当我在命令提示符下运行RunPort8080.bat文件时,请关闭命令提示符服务器不运行。问题
    Process p = Runtime.getRuntime().exec("TASKKILL /F /IM  communicator*"); 
    BufferedReader in = 
    new BufferedReader(new  InputStreamReader(p.getInputStream()));
    
        String result = null;  
        while ((result= in.readLine()) != null) {  
         if ( "SUCCESS".equals(result.substring(0,7))
         {
          break;
         }
        }  
    } catch (IOException e) 
    {  
       e.printStackTrace();  
    }  
    
    import org.jumpmind.symmetric.SymmetricWebServer;
    
    public class StartSymmetricEngine {
    
        /**
         * Start an engine that is configured by two properties files. One is
         * packaged with the application and contains overridden properties that are
         * specific to the application. The other is found in the application's
         * working directory. It can be used to setup environment specific
         * properties.
         */
        public static void main(String[] args) throws Exception {
    
            SymmetricWebServer node = new SymmetricWebServer(
                                       "classpath://my-application.properties");
    
            // this will create the database, sync triggers, start jobs running
            node.start(8080);
    
            // this will stop the node
            node.stop();
        }   
    
    }