Article View: borland.public.jbuilder.java.api
Article #2021Re: Problem terminate started process with the Runtime class
From: Fr�d�ric Lachasse
Date: Tue, 26 Jun 2001 04:47
Date: Tue, 26 Jun 2001 04:47
99 lines
4838 bytes
4838 bytes
"Kristian" <kristian@nr1.nu> wrote in message news:3b377c03_1@dnews...
> Hi!
>
> ...I would be very thankfull if someone could help me with the following
> problem with the Runtime class:
>
> I have created a "Process Launcher" in JBuilder which main task is to
start
> a process (a server program).
>
> The server needs a file (configuration purpose) as input and the output is
> usually stored in a file:
>
> *server configFile >>&logFile
>
> (Append to the logfile which exist and read both from stdout and stderr).
>
> So therefore I load an executable file and a configuration file in my GUI
> and after this
> I start this process and continously read from it and highlights different
> keywords etc.
>
> But my problem is that I can't destroy the process I just started?
(believe
> it or not ;-) )
>
> I'm using the Process class in Java because I thought it had nice
> functions that satisfied my requirements. Here is what it's look like:
>
> Start the process:
> proc = Runtime.getRuntime().exec(new String[] { "tcsh", "-c",
executableFile
> + " " + cfgFile + " >>&.logfile"});
> // Contiously read the logfile in a separate thread
> ....
> // When finished, try to destroy the process:
> proc.destroy();
>
> But the server process will not terminate...But this isn't a surprise
> because the only process that terminates is
> the tcshell.
> If I use it together with proc.waitFor(); , which should return 0 on
success
> it returns 15 in my case?
If you destroy() a process, it will not terminate correctly, so it will not
return 0. Doing waitFor() after calling destroy() is useless, except if you
need to wait for the complete termination of the process.
> But shouldn't there be a way to come around this problem and also manage
to
> terminate the second process, ie.
> the executableFile (the server in my case)?
Well, I do not like to use destroy() on a process. Like I said, the process
do not terminate normally, so you must be sure that it will handle well the
"crash". But may be you do not have a choice.
> Ofcourse, at the beginning, I didn't made it so hard for me and simple
> tried:
>
> proc = Runtime.getRuntime().exec(executableFile + " " + cfgFile + "
> >>&.logfile");
(Note: I recommand using "Runtime.getRuntime().exec(new String[] {
executableFile, cfgFile })" rather than
"Runtime.getRuntime().exec(executableFile + " " + cfgFile)". The former will
handle spaces in arguments properly).
> But it didn't succeed?
That is because ">>&logfile" is handle by tcsh, not by exec() or your
executable.
> As I said, I would be very glad if someone has a solution to this problem
> and could tell me (and others) about it.
> I know script files could be a solution but since this is the only problem
> left in my GUI program I think it would be
> nice to keep the rest of it as it is today. And hopefully, it is an easy
> problem to solve for someone who sees something
> that I have missed(?).
You missed the fact that the exec() handles the redirection of your
executable for you into pipes. So you just launch your program and read the
output through the other side of the pipes and do whatever you want with it:
write it into a log file and/or display the output on your console, with or
without highlighting the keywords.
So, you just get the the redirected InputStreams using
Process.getInputStream() for stdout and Process.getErrorStream() for stderr
and use Threads (one for each) to read these InputStreams and handle their
data.
Well, may be not that simple, as you need to handle these two InputStream's
in separate threads. But just using a synchronized method to handle a unit
of output should be enough to serialize and merge both inputs.
--
Fr�d�ric Lachasse (ECP 86)
flachasse@qwest.net
Technical Details
Message-ID:
<FxUZ6.2825$Ln6.535132@news.uswest.net>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!feed.news.qwest.net!news.uswest.net.POSTED!not-for-mailReferences:
<3b377c03_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 1089d0,83b38d347febae6b X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2001-06-25 21:51:52 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!feed.news.qwest.net!news.uswest.net.POSTED!not-for-mail From: "Fr�d�ric Lachasse" <flachasse@qwest.net> Newsgroups: borland.public.jbuilder.java.api References: <3b377c03_1@dnews> Subject: Re: Problem terminate started process with the Runtime class Lines: 101 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Message-ID: <FxUZ6.2825$Ln6.535132@news.uswest.net> Date: Mon, 25 Jun 2001 22:47:53 -0600 NNTP-Posting-Host: 63.231.85.172 X-Trace: news.uswest.net 993531109 63.231.85.172 (Mon, 25 Jun 2001 23:51:49 CDT) NNTP-Posting-Date: Mon, 25 Jun 2001 23:51:49 CDT