views:

1478

answers:

5

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

A: 

Using Runtime.exec is platform dependent.

If you're using windows try prepending:

cmd /c

to

java -jar .... etc. et

Something like:

...getRuntime().exec("cmd /c java -jar InputOutput....

See this also: Make system call and return stdout output.

OscarRyz
+1  A: 

See also Execute a Java program from our Java program

PhiLho
+1  A: 

Do you need to run the jar file in a different process? If not, you can write a Java program that invokes InputOutput.main(). Alternatively, if the name of the jar/class is only known at run-time, you can create a new class-loader, load the said class and invoke main() via reflection.

As for the redirection of input/output streams you can use System.setOut, setIn, setErr.

Itay
How would I redirect it in my code using setIn and setOut to get the jar input/output?
Jack L.
A: 

Now that you said that you need it to run in another process, here's the problem with your original code: You have a deadlock.

the process that you launch starts running and quickly fills its output and error streams. JarTest needs to regulary read their contents. Since you're doing it on a single thread sooner or later this thread will find itself waiting for data to be available on one the streams. At this point there's no one to collect data from the other stream.

Hence, you should launch dedicated threads to collect data from the standard streams of the other process.

Itay
How would you do this? I am not quite sure I understand. What do I put into a new thread?
Jack L.
A: 

All the information you need about process input/output threading are contained here: link

Chris