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.
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.
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.