使用Procrun从注册为Windows Service的Java运行批处理文件 [英] Running batch file from Java registered as Windows Service using Procrun

查看:294
本文介绍了使用Procrun从注册为Windows Service的Java运行批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例Java应用程序,我已使用 Procrun 注册为服务.我正在尝试从我的应用程序执行批处理文件

I have a sample Java application that I registered as a service using Procrun. 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();                    
            }
        }       
    }
}

启动服务时,它可以成功启动,但不能在Windows 7上启动批处理文件.

When I am starting the service, it gets started successfully but it does not launch batch file on my 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

推荐答案

要从Java应用程序执行批处理文件,请尝试以下代码:

To execute batch file from java application, try this piece of code:

//"D://bin/"是我的.bat的位置

// "D://bin/" is the location of my .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();
}

这篇关于使用Procrun从注册为Windows Service的Java运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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