• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Executing batch file from java

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic