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. 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.
Contents of Batch files are given below
Start.bat
@echo off
echo I am started
pause
Please let me know what am I missing her开发者_如何学Ce
Have you tried following
Runtime.getRuntime().exec("cmd /c start Start.bat");
To execute batch file from java application, try this piece of code:
// "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();
}
精彩评论