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.
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.
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?
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
Joined: Mar 25, 2007
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.
Joined: Oct 13, 2005
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.
subject: Need to run java processes in same session