• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need to run java processes in same session

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The article by Daconto is good, but since it was written the ProcessBuilder class was introduced, which makes the busines easier.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitesh and Rob, agree, thank you.
 
Rajiv Arora
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rajiv Arora
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic