exec() problem

L

Leigh Riley

I have a command line Java program that I run by typing: "java runExtrol
-i file.in -o file.out". I am writing a test class and would like to run
it from within Java using exec() e.g.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
String command = "java runExtrol -i file.in -o file.out";

try
{
Process px = Runtime.getRuntime().exec(command);
BufferedReader buf = new BufferedReader(new
InputStreamReader(px.getInputStream()));
String line;

while ((line = buf.readLine()) != null)
System.out.println(line);

px.waitFor();
px.destroy();
}

catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

The above code doesn't fail, but it doesn't output what runExtrol
should. When I replace the command with "ping", I get output back
telling me how to use ping properly. When I use "did" I get a
java.io.IOException: CreateProcess: dir error=2. I've tried fully
qulaifying the paths, but I still don't get any output. Can anyone think
of anything that I may be doing wrong?

Thanks very much in advance for any replies!!!

- Best regards,
Leigh
 
N

Neil Campbell

Leigh said:
I have a command line Java program that I run by typing: "java runExtrol
-i file.in -o file.out". I am writing a test class and would like to run
it from within Java using exec() e.g.

The above code doesn't fail, but it doesn't output what runExtrol
should. When I replace the command with "ping", I get output back
telling me how to use ping properly. When I use "did" I get a
java.io.IOException: CreateProcess: dir error=2. I've tried fully
qulaifying the paths, but I still don't get any output. Can anyone think
of anything that I may be doing wrong?

Hi,

A couple of suggestions:

If the command you want to run is written in Java, you can just run it by
calling its main() method with appropriate arguments. This also saves
creating a new JVM.

If you're running on Windows, which it looks like, I seem to recall that
there was a trick that had to do with creating a new shell and running the
command in that, so you say something like:

Runtime.getRuntime().exec("command.com /c " + command);

I think this helps if you want to use 'dir', because this isn't a program as
such, more a part of the command interpreter (or something). I can't
remember if /c is the right option though, if you look up the help for
command.com you should be able to find the right one.

Hope this helps.
 
S

Steve Horsley

I have a command line Java program that I run by typing: "java runExtrol
-i file.in -o file.out". I am writing a test class and would like to run
it from within Java using exec() e.g.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
String command = "java runExtrol -i file.in -o file.out";

try
{
Process px = Runtime.getRuntime().exec(command);
BufferedReader buf = new BufferedReader(new
InputStreamReader(px.getInputStream()));
String line;

while ((line = buf.readLine()) != null)
System.out.println(line);

px.waitFor();
px.destroy();
}

catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

The above code doesn't fail, but it doesn't output what runExtrol
should. When I replace the command with "ping", I get output back
telling me how to use ping properly. When I use "did" I get a
java.io.IOException: CreateProcess: dir error=2. I've tried fully
qulaifying the paths, but I still don't get any output. Can anyone think
of anything that I may be doing wrong?

Thanks very much in advance for any replies!!!

- Best regards,
Leigh

You that you should also be reading the process's error stream to prevent
it blocking on a write to a full buffer there. I thing you need another
thread or nio to be able to read both streams concurrently.

Steve
 
N

Neil Campbell

Leigh said:
I have a command line Java program that I run by typing: "java runExtrol
-i file.in -o file.out". I am writing a test class and would like to run
it from within Java using exec() e.g.

The above code doesn't fail, but it doesn't output what runExtrol
should. When I replace the command with "ping", I get output back
telling me how to use ping properly. When I use "did" I get a
java.io.IOException: CreateProcess: dir error=2. I've tried fully
qulaifying the paths, but I still don't get any output. Can anyone think
of anything that I may be doing wrong?

[Apologies if this appears twice.]

Hi,

A couple of suggestions:

If the command you want to run is written in Java, you can just run it by
calling its main() method with appropriate arguments. This also saves
creating a new JVM.

If you're running on Windows, which it looks like, I seem to recall that
there was a trick that had to do with creating a new shell and running the
command in that, so you say something like:

Runtime.getRuntime().exec("command.com /c " + command);

I think this helps if you want to use 'dir', because this isn't a program as
such, more a part of the command interpreter (or something). I can't
remember if /c is the right option though, if you look up the help for
command.com you should be able to find the right one.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top