Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1399
  • Last Modified:

Process Spawning and I/O redirection

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
tammana
Asked:
tammana
1 Solution
 
jpk041897Commented:
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
 
tammanaAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now