2

How can I write a program in Java that will execute another program? Also, the input of that program should be given from our program and the output of that program should be written into a file.

This is my small set of code to get its output:

Process p = Runtime.getRuntime().exec("C:\\j2sdk1.4.0\bin\\helloworld.java");
BufferedReader input =
 new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) 
 System.out.println(line);
input.close();

This was my set of code but this throws an IOException.

Michael Myers
193k47 gold badges301 silver badges297 bronze badges
asked Jan 21, 2009 at 8:46
1
  • just curious...what are the "**" for? Commented Jan 22, 2009 at 4:10

5 Answers 5

5

The API that Java offers for this is the ProcessBuilder. It is relatively straightforward to set working directory and pass parameters.

What is a little tricky is passing STDIN and reading STDERR and STDOUT, at least for non-trivial sizes thereof, because you need to start seperate threads to make sure the respective buffers get cleared. Otherwise the application that you called might block until it can write more output, and if you also wait for that process to finish (without making sure that STDOUT gets read), you will deadlock.

answered Jan 21, 2009 at 8:51
Sign up to request clarification or add additional context in comments.

Comments

4

You can use java.lang.Process and java.lang.ProcessBuilder. You interact with the input/output of the process using getInputStream/getOutputStream/getErrorStream.

However, there's an Apache Commons library called Exec which is designed to make all of this easier. (It can normally get quite hairy when it comes to quoting command line parameters etc.) I haven't used Exec myself, but it's worth checking out.

answered Jan 21, 2009 at 8:49

Comments

4

When you only want to start other programms, you can use the exec method like this:

Runtime r = Runtime.getRuntime();
mStartProcess = r.exec(applicationName, null, fileToExecute);
StreamLogger outputGobbler = new StreamLogger(mStartProcess.getInputStream());
outputGobbler.start();
int returnCode = mStartProcess.waitFor();
class StreamLogger extends Thread{
 private InputStream mInputStream;
 public StreamLogger(InputStream is) {
 this.mInputStream = is;
 }
 public void run() {
 try {
 InputStreamReader isr = new InputStreamReader(mInputStream);
 BufferedReader br = new BufferedReader(isr);
 String line = null;
 while ((line = br.readLine()) != null) {
 System.out.println(line);
 }
 } catch (IOException ioe) {
 ioe.printStackTrace();
 }
 }
}

exec:

public Process exec(String command, String envp[], File dir) 
 @param command a specified system command.
 @param envp array of strings, each element of which 
 has environment variable settings in format
 <i>name</i>=<i>value</i>.
 @param dir the working directory of the subprocess, or
 <tt>null</tt> if the subprocess should inherit
 the working directory of the current process.
answered Jan 21, 2009 at 9:23

Comments

2

Please do not edit your question so that it does not fit the original answers anymore. If you have follow-up question, clearly mark them as such, or ask them as a seperate questions, or use comments or something.

As for your IOException, please give the error message it shows.

Also, it seems as if you are trying to run a ".java" file directly. That will not work. The methods described here are to launch native binary executables. If you want to run a ".java" file, you have to compile it to a class, and the invoke that class' main method.

answered Jan 22, 2009 at 4:00

Comments

1

What platform are you in?

If you are on *nix you can type:

java MyProgram | myexternalprogram> myfilename.txt

answered Jan 21, 2009 at 8:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.