• 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

Launch batch file from java?

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been trying to launch the batch file using :

String cmd = "c:\\here.bat";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);

Nothing fires though. When I put notepad where cmd is it launches just fine. But when I keep my cmd the same and try and launch it runs the command and keeps running my java code. The bat file should have a printout screen where it displays certain values. This is not happening.

In my code I do have try/catch blocks.

Any ideas?

Thanks,
Randy
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The standard input and output of the process are connected to OutputStreams and InputStreams, respectively, that you can get from the Process object returned by the exec() command; they're not connected to the terminal. If you want the output of your batch file to be printed, then you have to use getInputStream() to get the process's output from the Process object, and then in your Java code read the data and print it yourself.

The reason you can see Notepad is because, of course, Notepad doesn't use its standard output -- instead it throws up a GUI.
 
Tempora Telora
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I am saying though is that I feel that my batch file is not being launched. Is my call wrong?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do you think it is not running? is something not done (a file not created, an email not sent) that you think should be? or do you think it's not because you don't see anything on your screen?

if it's the latter, then re-read what Ernest said. the .bat file COULD be running, but if all it does it print something, you're not gonna see it unless you get that InputStream.

[edit]

i went back and re-read your original post...

what is (most likely) happening is that your script IS running. but the output from your script is not going to the screen, like EFH said. that's why you're not seeing it. it's going to your .bat file's output stream, which is not attatched to your screen. it is effectively going nowhere.

follow EFH's advice. get the stream. read from the stream, and print it out in your java code.
[ October 26, 2006: Message edited by: fred rosenberger ]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should write your Java code like below:
String cmd = "cmd /c c:\\here.bat";
Then the runtime class will run the CMD process for you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic