Reputation: 1219
I have to execute a xyz.cmd
file which is in a directory E:/abc
. So the absolute path of the file to be executed is E:/abc/xyz.cmd
. When executed, a new window is created by the file itself.
My code snippet is:-
String path = “E:\\abc”;
String cmd = path + “\\xyz.cmd”;
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
processBuilder.redirectErrorStream(true);
processBuilder.directory(new File(path));
processBuilder.start();
This does not work, but gives no error or exception. But the cmd file works fine, it can be executed manually from its directory using explorer or cmd-prompt. Tried using different versions of jdk, but in vain. I am using windows 7 OS. I do not see the process running in the Task Manager also. Any idea what is going wrong? The same code works fine in a different computer with the same config.
===EDIT==== Can this be a security issue? Something like the user executing the program is not having enough priveleges to execute a file?
Upvotes: 0
Views: 1675
Reputation: 285405
You need to call cmd.exe
as first part of your process builder String in order for the command processor to be able to call the .cmd file. This is also true for .bat files, or any OS type command. For example, please look here.
Also, please look here: When Runtime.exec() won't
Edit
You state:
please understand, this is not the problem of not adding cmd.exe in the processbuilder; because of the previous commands, cmd.exe will be taken care.
I see no documentation in your posts so far that this is true, and all my experience strongly suggests otherwise.
You also state:
Can this be a security issue? Something like the user executing the program is not having enough priveleges to execute a file?
No way to know unless you capture and display the process's input stream. In fact if you don't capture this stream, you could prevent your process from functioning at all. Often we have to also capture the error stream as well, but you've combined them with
processBuilder.redirectErrorStream(true)
Please read my "When Runtime.exec() won't" link above for more on the necessity of capturing streams.
Upvotes: 2