Solved

Process Spawning and I/O redirection

Posted on 1997-05-06
2
1,384 Views
Last Modified: 2006-11-17
I am trying to do the following but can't seem to find a way to
do it in java. I am looking for suggestions ...

I am looking for a way to spawn a process and redirect the stdout
and stderr to a file. What I did in C/C++ was to create a file
in the parent process and manipulate the stdout and stderr (using
dup on unix and the win32 CreateProcess API on NT) and then spawn
the child. Is there a way to achieve this ?

Note:
The child process is a legacy app (I don't have the source code).
I just want to launch it and make its stdout and stderr go to a
flat file.

Also, after spawning the process, I am looking for a way to verify
if the process is running or terminated. Is there anyway to do this ?
I have the process handle/class that was returned when the process
was spawned. I couldn't find an API call for that either.

I guess if I can do the above, I will look for more :-) facilities
like retrieving environment variables and setting uid's etc. For
now, I'd like to start with these.

Any help appreciated.

Thanks,
-krishna
0
Comment
Question by:tammana
2 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 25 total points
Comment Utility
java.comp.lang's exec method will fork and spawn a process. Here are its options:

Process exec(String command) throws IOException

·      Executes the specified command.

Process exec(String cmdarray[]) throws IOException

·      Executes the commands found in the cmdarray.

Process exec(String cmdarray[], String envp[]) throws IOException

·      Executes the commands in the cmdarray using the environment parameters specified in the envp array.
·      The parameters should be specified in the format parameter = value.

Process exec(String command, String envp[]) throws IOException

·      Executes the command using the environment parameters specified in the envp array.
·      The parameters should be specified in the format parameter = value.

Under UNIX you could call:

exec("YouProgram > xx 2> error");

Under windows it won't be that easy. DOS only supports 1 output redirection stream (either stdout or stderr).

A couple of possible workarrounds are to either download a tee command (from http://www.shareware.com and search for unix in the DOS section). Or somehow hooking into your app and redirecting file handle # 3 (stderr). If its a DOS program the second solution is not possible.

To determine if the process still exists, you are going to have to use native methods in windows. (Obtain the window handle and then poll the task list to see if its still existing) or create a program in C that calls WinExec and monitors for a Child termination Message.

In UNIX you could always dish out a shell script that greps your pid in the result set for ps. Something in the likes of:

ps -elf | grep $1

and evalueate the result to determine if the process is still active.

Retrieving environment variables is a mess. Sun decided to remove that option in JDK 1.01 for reasons I am still unable to panthom. There is a way to work arround this using the JAVA command parameters but its messy and doesn't work all that great for DOS.

Regarding  uid's and other OS dependant issues, you will either have to use native calls or use exec() to shell out to the OS.
0
 

Author Comment

by:tammana
Comment Utility
JPK, I am looking for platform independence when I move to java.
I already have the app in native (C) code. If I have to have
platform specific code, there is not much benefit for me.

Anyway, I was looking more for an equivalent of "dup" call
under UNIX which will allow me to dup the file handles 1 & 2
(stdout and stderr) to point to a file before spawning. I was
hoping that java had such a call. NT allows this also in the
Win32 API.

Oh well! Thanks for the answer.

-krishna


0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now