Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problems in executing a process

Reply
Thread Tools

Problems in executing a process

 
 
asd
Guest
Posts: n/a
 
      06-17-2005
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

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      06-17-2005
On 17 Jun 2005 01:00:53 -0700, asd wrote:
> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
asd
Guest
Posts: n/a
 
      06-17-2005
>Either consume the contents of the output and error streams
How do I do this?

Please help me out.

regards,

ASD

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      06-17-2005
On 17 Jun 2005 01:59:52 -0700, asd wrote:
>>Either consume the contents of the output and error streams

> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
John Currier
Guest
Posts: n/a
 
      06-17-2005
Didn't they implement something in 1.5 so you don't have to go though
that mess?

John

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Process.Start... started process not executing any code jpock76 ASP .Net 0 08-23-2010 02:16 PM
Access denied when executing Process.Start() on my Server 2003 CÚdric RossÚ ASP .Net 3 09-23-2008 08:31 AM
Prevent worker process from recycling when foreground thread is executing. Marcel van den Hof ASP .Net 3 08-11-2005 11:02 PM
Process is not executing Pratibha ASP .Net 2 05-26-2005 09:59 AM
Executing a process in a running app from a web page Buddy Ackerman ASP .Net 1 05-11-2005 04:06 PM



Advertisments