Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Process Spawning and I/O redirection

Posted on 1997-05-06
2
Medium Priority
?
1,397 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 50 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

715 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