Problems in executing a process

A

asd

Hi all,

I am trying to execute a cpp application (called iosconvert) from java
code. The details are as follows:

Runtime rtime = Runtime.getRuntime();

String str_ios = new String[]("iosconvert", "/home/arvind/cisco2.conf",
"/home/arvind/nct1/bin/cisco2.xml");

proc = rtime.exec(str_ios);

int exitVal = proc.waitFor();

System.out.println("exit = "+exitVal);

Iosconvert takes two parameters, which are passed. Now when I run this
code it hangs at proc.waitFor().

Since I am executing the code on CLI I have to do a CTRL+C. The exit
value that gets printed is 129.

On the other hand when I execute the cpp application directly from CLI
it works fine.

Thanks in advance for any help.
I am using j2re 1.4.2 on Linux env.

regards,
ASD
 
G

Gordon Beaton

I am trying to execute a cpp application (called iosconvert) from
java code. The details are as follows:
[...]

Now when I run this code it hangs at proc.waitFor().

Because you are not reading the output produced by the child process,
its output streams block and the process is prevented from running to
completion.

Either consume the contents of the output and error streams while the
process is running, or specify the command so that no output is sent
to these streams (by specifying a flag to isoconvert, or using a shell
and redirection).

/gordon
 
A

asd

Either consume the contents of the output and error streams
How do I do this?

Please help me out.

regards,

ASD
 
G

Gordon Beaton

How do I do this?

Runtime.exec() returns a Process object. Obtain the output and error
streams from that.

Read each of those streams until you reach EOF. Note that you need to
read both streams concurrently, so you will need an additional thread
for one of them.

If you *know* that the process will not write anything to its error
stream, you can cheat a little, and not read from that stream. That
also removes the need for the additional thread.

Finally, after reaching EOF on the streams, wait for the process to
terminate and get the exit value.

A simpler (but platform specific) solution is to let a shell do the
work for you. Specify the command line as an array of 3 Strings, like
this:

String[] cmd = {
"/bin/sh",
"-c",
"iosconvert /home/arvind/cisco2.conf /home/arvind/nct1/bin/cisco2.xml"
+ " > /dev/null 2>&1"
}

It's important to group the command into 3 Strings exactly the way
I've shown here (I broke the last one only to avoid line wrapping in
my post).

/gordon
 
J

John Currier

Didn't they implement something in 1.5 so you don't have to go though
that mess?

John
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top