• Post Reply Bookmark Topic Watch Topic
  • New Topic

Executing batch file from java  RSS feed

 
Abhishek Mish
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I need to execute a batch file from java, below is the code snippet

Runtime rt = Runtime.getRuntime();
Process pr=rt.exec("cmd /C start D:\\batchRun\\test.bat");
int exitValue=pr.exitValue();

I am able to successfully execute the batch file, but i have a few concerns,

1. Also when the batch is completed successfully i want to know if it there were any errors in the batch execution. I tried using
pr.exitValue() and pr.waitFor(), but in both cases, even if i explicity close the command propmt i still obtain exitValue as 0.
If there anyways where i can get to know whether batch file completed without errors.

2. How to execute batch file silently in the background without cmmmand prompt getting open. I removed cmd /C start and it works fine,
but this behaviour is not consistent in different OS.

Thanks,
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you will get a proper answer from someone else, I suggest you search for and read the Javaworld article "When Runtime.exec() won't".
 
Abhishek Mish
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob,
Its nice and informative info, I am sure it will definitely help me.
I will go through and will post if any problem i faced.

Thanks,
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!