Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Problems in executing a process

Thread view: 
asd - 17 Jun 2005 09:00 GMT
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
Gordon Beaton - 17 Jun 2005 09:12 GMT
> 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

Signature

[  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

asd - 17 Jun 2005 09:59 GMT
>Either consume the contents of the output and error streams
How do I do this?

Please help me out.

regards,

ASD
Gordon Beaton - 17 Jun 2005 10:32 GMT
>>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

Signature

[  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

John Currier - 17 Jun 2005 19:41 GMT
Didn't they implement something in 1.5 so you don't have to go though
that mess?

John


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.