• 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

How to send comands to anoyther program

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Elias Medeiros
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, thanks.
 
Elias Medeiros
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic