Can Java Runtime.exec another java program that uses stdin?
Asked Answered
H

1

1

I have run into an issue where , when using Java Runtime to run another java program, the program freezes because the other program requires stdin . There is a problem with handling the stdin after executing another java program with Runtime exec() .

Here is sample code that I can't get to work. Is this even possible?

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

public class ExecNoGobble
{
    public static void main(String args[])
    {
        if (args.length < 1)
        {
            System.out.println("USAGE: java ExecNoGobble <cmd>");
            System.exit(1);
        }

        try
        {            
            String[] cmd = new String[3];
                cmd[0] = "cmd.exe" ;
                cmd[1] = "/C" ;
                cmd[2] = args[0];
            Runtime rt = Runtime.getRuntime();
            System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);
            Process proc = rt.exec(cmd);
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);        
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}

And the ReadInput.java file:

import java.io.*;

public class ReadInput {

   public static void main (String[] args) {

      //  prompt the user to enter their name
      System.out.print("Enter your name: ");

      //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String userName = null;

      //  read the username from the command-line; need to use try/catch with the
      //  readLine() method
      try {
         userName = br.readLine();
      } catch (IOException ioe) {
         System.out.println("IO error trying to read your name!");
         System.exit(1);
      }

      System.out.println("Thanks for the name, " + userName);

   }

}  // end of ReadInput class

And, finally , the batch file that launches it:

@echo off

echo.
echo Try to run a program  (click a key to continue)
echo.
pause>nul
java ExecNoGobble "java -cp . ReadInput"
echo.
echo (click a key to end)
pause>nul

I also posted the question here: http://forums.oracle.com/forums/message.jspa?messageID=9747449

Highbred answered 21/7, 2011 at 17:56 Comment(2)
If both are java, why don't you just call the main function inside your external java program? Just instantiate that java program object and call its functions. sounds not making sense to execute a java program from another program.Minute
I believe I understand what you are saying: I would need to pass JVM args and classpath to my Exec program to support loading those packages and executing the main class?Highbred
I
3

Call the method Process.getOutputStream and feed your input to the returned output stream.

api docs:

public abstract OutputStream getOutputStream()

Gets the output stream of the subprocess. Output to the stream is piped into the standard input stream of the process represented by this Process object.

Implementation note: It is a good idea for the output stream to be buffered.

Returns:

the output stream connected to the normal input of the subprocess.

Iamb answered 21/7, 2011 at 18:16 Comment(3)
and make sure you are reading from both stdout and stderr streamsAdz
@Jochen: indeed, he will need to do that if he wants to see his output. But I'll wait and see if he needs more help before adding anything further.Iamb
some operation systems only have limited buffer space for I/O between processes and will block if you are not reading the output streamsAdz

© 2022 - 2024 — McMap. All rights reserved.