GeeCON Prague 2014*
The moose likes I/O and Streams and the fly likes How to send comands to anoyther program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to send comands to anoyther program" Watch "How to send comands to anoyther program" New topic
Author

How to send comands to anoyther program

Elias Medeiros
Greenhorn

Joined: May 11, 2006
Posts: 4
I need to execute a program done in haskell that is usually executed from command line.
This program wait for an input and shows the correspondent output.
For instance:

$ prosoft-reduce
prelude> / in Integers
=> now in the context of Integers
Integers> succ(one)
=> succ(succ(zero)
Integers>
$

My program must call this program "prosoft-reduce", send commands to it ("/ in Integers",
"succ(one)", for example) and read the output ("=> now in the context of Integers", "=> succ(succ(zero))").

How can I do it?

Thanks,
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
See ProcessBuilder in Java5 or System.runtime.exec() in earlier JDKs. These let you start another program as its own process. You can connect your own streams to the stdin, stdout and errout of the other process and read and write. This bit gets tricky as it works best with a separate thread for each stream. But once you get it all hung together you should be able to send it commands and read the responses.

See how far you get with this on your own. Post some code so we can see where you're stuck (if you get stuck) and we'll try to keep you moving along.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Elias Medeiros
Greenhorn

Joined: May 11, 2006
Posts: 4
I need write in the input "input" of process "p" and read from output "output" of process "p", right?
But I only can read from an InputStream and I only can write in an outputStream. How can I solve this problem?



import java.io.*;
import java.util.Properties;


public class Teste2 {
public static void main (String args[])
{
Process p;
InputStream input,inputErr;
OutputStream output;

try{
p = Runtime.getRuntime().exec("prosoft-reduce");

input = p.getInputStream();
output = p.getOutputStream();
inputErr = p.getErrorStream();

}
catch(IOException e)
{
e.printStackTrace();
}
System.exit(0);
}
}
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by Elias Medeiros:
I need write in the input "input" of process "p" and read from output "output" of process "p", right?
Wrong.
But I only can read from an InputStream and I only can write in an outputStream. How can I solve this problem?
By reading from the InputStream (which process "p" is writing to as stdout) and by writing to the OutputStream (which process "p" is reading from as stdin).
Elias Medeiros
Greenhorn

Joined: May 11, 2006
Posts: 4
ok, thanks.
Elias Medeiros
Greenhorn

Joined: May 11, 2006
Posts: 4
only more one question:
I write in the output several times, but the input only can be read when the process "p" is finished (command "\n").
How can I do to allow the reading of input at each writing in the output?

thanks,


code for test only:


public class Teste {
public static void main (String args[])
{
Process p;
InputStream input,inputErr;
OutputStream output;


try{
p = Runtime.getRuntime().exec("prosoft-reduce");

input = p.getInputStream();
output = p.getOutputStream();
inputErr = p.getErrorStream();

InputStreamReader reader = new InputStreamReader(input);
BufferedReader bufferReader = new BufferedReader(reader);

OutputStreamWriter writer = new OutputStreamWriter(output);
BufferedWriter bufferWriter = new BufferedWriter(writer);

InputStreamReader errorReader = new InputStreamReader(inputErr);
BufferedReader errorBufferReader = new BufferedReader(errorReader);


String command;
String line = "";

command = JOptionPane.showInputDialog(null,"command");

while (!command.equals("")) {
bufferWriter.write(command+"\n");
bufferWriter.flush();

/*while (line!=null){
System.out.println(line);
line = bufferReader.readLine();
}*/

System.out.println(line);
command = JOptionPane.showInputDialog(null,"command");
}

bufferWriter.write("\n");
bufferWriter.flush();

line = "";
while (line!=null){
System.out.println(line);
line = bufferReader.readLine();
}

line = "";
while (line!=null){
System.out.println(line);
line = errorBufferReader.readLine();
}

}
catch(IOException e)
{
e.printStackTrace();
}

System.exit(0);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to send comands to anoyther program