Jump to content
  • Advertisement
Sign in to follow this  

Calling programs from within java

This topic is 4346 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, does anyone here know a general method to start a process from within java, but to redirect the output of the program in real time (i.e. while it is still running) to, say, a textbox component in a GUI? i.e. suppose for some reason i'd want to call a program that outputs the time to stdout every 2 minutes. how would i start the program from a java class, but make sure the output of the program is handled by my java class, and not by opening a terminal window? any help will be appreciated. edit; actually, a more clear example would be something like this: i want to be able to implement a feature using Java similar to that in many IDE for programming languages in which you can run your code and have the output captured within the IDE. [Edited by - arkmaster on June 26, 2006 2:21:03 PM]

Share this post


Link to post
Share on other sites
Advertisement
java.lang.Runtime has a few overloaded versions of an exec function that can execute a new process.

Getting the output of the exectued process ive never done... but looking at the documentation you can try the following:

The exec functions return a Process object (part of java.lang.Process). The process object then has a getInputStream function which returns an InputStream (java.io.InputStream) object which I believe would most likely contain the output from your process. This object allows you to read the bytes one by one which you would then have to cast to characters.

Hope that helps a little or at least gets you goin in the proper direction.

Share this post


Link to post
Share on other sites
Oh, so is Runtime.exec(...) non-blocking? i.e. it doesn't wait for the process to terminate before passing control back to the JVM?

Share this post


Link to post
Share on other sites
Quote:
The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.


Thats from the documentation... therefore Id assume it doesnt wait for the process to terminate before passing control back to JVM if you are supposed to be able to directly control the exectued process through the returned Process object....
[edit]thus if you would like to interact with the process in the background without having your java app wait, then you might want to consider putting it in a thread of its own.

You should download the java documentation for whichever version you are using as its extremely helpful.

J2SE 1.5.0 API
J2SE 1.4.2 API

[Edited by - ju2wheels on June 27, 2006 3:51:17 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!