Solved

Process Spawning and I/O redirection

Posted on 1997-05-06
2
1,393 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
ID: 1220205
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
ID: 1220206
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Arrays.asList  VS  ArrayList 4 71
Oracle SQL syntax check  without executing 6 74
ejb on wildfly 5 30
add projects t working set in maven 2 16
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 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