wood burning stoves 2.0*
The moose likes Java in General and the fly likes Need to run java processes in same session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need to run java processes in same session" Watch "Need to run java processes in same session" New topic
Author

Need to run java processes in same session

Rajiv Arora
Greenhorn

Joined: Mar 25, 2007
Posts: 26
I need to invoke native Processes in a session using Java.

To summarize the problem, I am using ProcessBuilder class to invoke commands. But challenge is can I invoke subsequent commands in the same session?

i.e. once you have got an instance of Process from ProcessBuilder, we want to execute a series of commands in the same process (session).

Just to explain by this with an example:

Say, we are in C:\
And we execute �dir� command. It will list the directories in C:\
Now we want to execute �cd C:\test� and then do �dir� again so that we get the listing of �C:\test� and not �C:\�

The problem that I am facing with this is that:
- On execution of first command I get the result back in the Process's Inputstream but when I write the second command to the Outputstream of the same process to be executed in the same session, then I do not get the result back in the Process's InputStream.

is there anything that I am doing wrong? Please help me understand this.

Thanks a lot.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

This article elaborates few nuances of using Runtime.exec() and Process. It will be helpful if you are working with Process. In fact the article gives code for running a dir command and reading its output.
Are you draining the error stream of the process? It may be that you are getting an error and hence nothing on the process' InputStream.
Also, is it possible to run a script that has all the commands that you need to run, rather than running one command at a time.


apigee, a better way to API!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
The article by Daconto is good, but since it was written the ProcessBuilder class was introduced, which makes the busines easier.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by Campbell Ritchie:
The article by Daconto is good, but since it was written the ProcessBuilder class was introduced, which makes the busines easier.


True but the facts related to Process, like draining the streams, remains the same.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

The only advantage ProcessBuilder has is its easy way to redirect the process' error stream to its output stream, after which you only have to read one stream.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Nitesh and Rob, agree, thank you.
Rajiv Arora
Greenhorn

Joined: Mar 25, 2007
Posts: 26
Thanks a lot everyone for your responses !!

I have tried using ProcessBuilder also and the present issues that I am facing to execute all the commands in the same session are:

1) I am trying to go to a subshell first of all - using the "command" command as first thing, by spawning the process as:
cmd.exe /K command

2) Now with this my process goes into a subshell and that I know by the data that I get in the parent Process's InputStream ("Command" command's outputstream sends it to Parent process's inputstream).

3) Next I send the command "cp c:\1.txt C:\test\1.txt" to my parent process's outputstream which ultimately goes to my "command" command's inputstream and gets executed. This I know because the file is actually copied. But even then I do not get the "1 file(s) copied" output back in my Parent process's Inputstream; but my requirement is to capture that.

The reason I believe for this is that - the "cp" command that we executed in the "command" command's subshell sends back the response in "command"'s InputStream. But we do not have any handle to it. The only handle that we have is Parent process, i.e. "cmd.exe"'s I/O streams.

So is there any way that I can use to capture the output of the commands executed in a subshell?

Thanks a lot everyone for your help !!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3168
    
  10
Could you not redirect the output of the cp command to a file and then read that
cp c:\1.txt C:\test\1.txt > stdout.txt 2>stderr.txt


Joanne
Rajiv Arora
Greenhorn

Joined: Mar 25, 2007
Posts: 26
Redircting the output would not be a good option as in our system there will be thousands of commands running. So I would definitely prefer an approach where I can get hold of the I/O Streams of a process in a sub-shell.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Redirect the output streams (ProcessBuilder allows you to combine them) by setting up a Scanner to read the streams, and a List<String> to add any messages to. When you have finished the process, you can use the List however you need to.

Check carefully whether that works; it is from memory and I might have given some of the method names wrongly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to run java processes in same session
 
Similar Threads
Getting the output from a running (unterminated) process via the inputStream
How to get Output from command prompt into java console
What does this error mean??
Help with runtime.exe and javac from command line
Catching InputStream from Runtime/Process