Reputation: 3
For educational purpouses I am asked to use Java to call the execution of a .bat trough cmd.
As a starting point I did this little code, that for what I know should work, but executing the class does nothing, while running the .bat works as expected.
Java:
Runtime.getRuntime().exec("cmd /c start myDir.bat");
Content of myDir.bat (which is located in the same folder of the Java class):
dir > file_list.txt
Double click on the .bat generates and fills the file.
Upvotes: 0
Views: 79
Reputation: 74
you can just execute : Runtime.getRuntime().exec("myDir.bat"); or provide absolutePath to your bat file. You can also get OUTPUT and OUPUT ERROR from myDir.bat with java Thread.
If you are more than one parameter, you put an array string on exec method, example: String[] p = {"cmd","/c","start","myDir.bat"}; Runtime.getRuntime().exec(p);
Upvotes: 1