Getting input and output from a jar file run from java class?
Posted
by Jack L.
on Stack Overflow
See other posts from Stack Overflow
or by Jack L.
Published on 2009-02-14T04:50:32Z
Indexed on
2010/04/24
14:43 UTC
Read the original article
Hit count: 299
Hi,
I have a jar file that runs this code:
public class InputOutput {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
boolean cont = true;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (cont) {
System.out.print("Input something: ");
String temp = in.readLine();
if (temp.equals("end")) {
cont = false;
System.out.println("Terminated.");
}
else
System.out.println(temp);
}
}
}
I want to program another java class that executes this jar file and can get the input and send output to it. Is it possible? The current code I have is this but it is not working:
public class JarTest {
/**
* Test input and output of jar files
* @author Jack
*/
public static void main(String[] args) {
try {
Process io = Runtime.getRuntime().exec("java -jar InputOutput.jar");
BufferedReader in = new BufferedReader(new InputStreamReader(io.getInputStream()));
OutputStreamWriter out = new OutputStreamWriter(io.getOutputStream());
boolean cont = true;
BufferedReader consolein = new BufferedReader(new InputStreamReader(System.in));
while (cont) {
String temp = consolein.readLine();
out.write(temp);
System.out.println(in.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks for your help
© Stack Overflow or respective owner