运行批处理文件时出现 java.lang.InterruptedException

问题描述

我正在尝试运行一个批处理文件,其中包含要重新启动的一组服务。当我从 Java 应用程序调用此批处理文件时,我收到中断异常。

我的批处理文件

call net stop app-service
call net start app-service
call net stop ui-service
call net start ui-service
call net stop custom-app
call net start custom-app
call net stop MSsqlSERVER /Y
call net start MSsqlSERVER

我运行批处理文件的java代码

    File file = new File(System.getProperty("user.dir") + File.separator + "restart.bat");
    String time="cmd /c "+file.getAbsolutePath();
    Process p = Runtime.getRuntime().exec(command);         
    int exitvalue = p.waitFor();

我收到以下错误

java.lang.InterruptedException
at java.lang.ProcessImpl.waitFor(ProcessImpl.java:451)

我做错了什么?

解决方法

看起来问题出在批处理文件上。立即调用停止和启动是导致问题的原因。确保 SQL 服务器和服务已停止,然后启动服务器和那些服务。

请检查此答案:

Stop and Start a service via batch or cmd file

使用SC(服务控制)命令,它给你更多的选择 不仅仅是开始和停止。

说明: SC 是一个命令行程序,用于与 NT 服务控制器和服务。用法: sc [命令] [服务名称] ...

  The option <server> has the form "\\ServerName"
  Further help on commands can be obtained by typing: "sc [command]"

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...