Solved

Process Spawning and I/O redirection

Posted on 1997-05-06
2
1,395 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 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 83
Adding multiple JVM environments to RedHat 6 7 81
web project error add remove 1 83
Java List 4 76
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

734 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