简体   繁体   English

使用Procrun从注册为Windows Service的Java运行批处理文件

[英]Running batch file from Java registered as Windows Service using Procrun

I have a sample Java application that I registered as a service using Procrun . 我有一个使用Procrun注册为服务的示例Java应用程序。 I am trying to execute Batch file from my application 我正在尝试从应用程序执行批处理文件

public class Service {
    public static void main(String args[]) throws IOException, InterruptedException {       
        if(args.length>0){
            if(args[0].equals("start")){
                ProcessBuilder builder = new 
                    ProcessBuilder("cmd","/c","start","Start.bat");
                builder.start();                
            }else if(args[0].equals("shutdown")){
                ProcessBuilder builder = new 
                    ProcessBuilder("cmd","/c","start","Stop.bat");
                builder.start();                    
            }
        }       
    }
}

When I am starting the service, it gets started successfully but it does not launch batch file on my Windows 7. 当我启动该服务时,它会成功启动,但不会在Windows 7上启动批处理文件。

Contents of Batch files are given below 批处理文件的内容如下

Start.bat Start.bat

@echo off
echo I am started
pause

Please let me know what am I missing here 请让我知道我在这里想念什么

To execute batch file from java application, try this piece of code: 要从Java应用程序执行批处理文件,请尝试以下代码:

// "D://bin/" is the location of my .bat //“ D:// bin /”是我的.bat的位置

   File dir = new File("D:/bin/");
try {

    // sign.bat if my actual file.
    Runtime.getRuntime().exec("cmd.exe /c sign.bat", null, dir);   
      try {

        Thread.sleep(100);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

您是否尝试过追踪

Runtime.getRuntime().exec("cmd /c start Start.bat");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM