• Post Reply Bookmark Topic Watch Topic
  • New Topic

process execution in linux  RSS feed

 
Ashok
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I have got a problem with executing process from java.
I want to open a shell from java and execute a command. It is easy in this way
Process application = Runtime.getRuntime().exec("konsole <command>");

But I use a special shell (trigo shell) which can not take commands in this way. I have to open it and type the command.

Can anyone suggest me a way open the shell and execute a command through java.

Chaitanya
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your shell doesn't take command line arguments? The Java API Documentation says the following about java.lang.Process:

The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess.

As long as your shell uses standard in, out, and error, you should be able to send commands to it using an OutputStream. Don't forget to send a line terminator with your command, otherwise it's like you are typing a command and not hitting the "enter" key.
 
Ashok
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion Joe.
As I am a new bie can you tell me how to do this ..i mean sending command to an output stream with an example(konsole)

Chaitanya
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reading from and writing to streams in Java is the same no matter if the stream is a file, socket or a process. You are familiar with System.out.println(), correct? The out member of System is a PrintStream. Process gives you an OutputStream which is pretty primitive (you can only write a single byte or an array of bytes). Rather than converting everything I want to write to bytes, I prefer to wrap the stream with a higher-level stream like PrintWriter which can write String objects directly, like so:

Read through the Java Tutorial chapter on Input/Output for more on working with streams.
 
Ashok
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much joe.
Chaitanya
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!