简体   繁体   中英

Running a java program using Runtime.exec()?

Im creating a java IDE application and have encountered a problem with running the users code. Currently i am using Runtime.exec() to compile and then run the program while using the InputStream and ErrorStream to generate the output. Which works fine if the program never stops to ask for input from the user. In this case the program will crash and does not display any output. I think this is because the readers will not read the output untill the program has finished running.

I have tried using threads in order to get around this but with no success. Is there any way to pause the program during run time to allow user input or is there any other way that i could create a console to run the users code?

I think this is because the readers will not read the output untill the program has finished running.

I don't know about exactly how you would get the process to "wait" until user input is written to the output stream of the process, but I can help clarify some things for you.

I know that the readers certainly do not wait until the program finishes running to read the output stream.

        LoggingProcessWrapperUtil.myPB = new ProcessBuilder(command);
        LoggingProcessWrapperUtil.myPB.redirectInput(Redirect.PIPE);
        LoggingProcessWrapperUtil.myPB.redirectOutput(Redirect.PIPE);

        StringBuilder commandStr = new StringBuilder();

        for (String str : command) {
            commandStr.append(str);
            commandStr.append(" ");
        }

        LoggingProcessWrapperUtil.log.debug("Executing command : {}", commandStr);

        Process p = LoggingProcessWrapperUtil.myPB.start();

        LoggingProcessWrapperUtil.log.debug("Starting ProcessStreamWorker pointed at process");

        for (String str : args_to_write_on_stream) {
            LoggingProcessWrapperUtil.log.debug("Writing arguments on inbound outputstream:");
            LoggingProcessWrapperUtil.log.debug(str);
        }

        ProcessStreamWorker w = new ProcessStreamWorker(p, args_to_write_on_stream);

        new Thread(w).start();

        int exitCode = w.waitOn(timeoutMS);

        SimpleEntry<Integer, String> returnEntry = new SimpleEntry<Integer, String>(exitCode,
                w.getAggregateOutput());

        return returnEntry;

This short snippet was my solution to a related problem, albeit I was looking to automate a process, not pause it for user input. I build the process, start it, and then immediately put it into a runnable process wrapper that appends the arguments onto the OutputStream of the process. This has worked for me, so the process output stream certainly does get read in realtime.

One thing that you can try is ProcessBuilder.redirectInput and ProcessBuilder.redirectOutput(Redirect.Inherit), if you have a CLI java app launching another CLI app then the process you are launching should also accept input/output activity from the console window that your main app is running on.

If you'd like there is another way to do what you requested. You could redirect the input stream of your process (jdk 1.7 is required for this code):

try {
     ProcessBuilder pb = new ProcessBuilder("cmd");  
     pb.redirectErrorStream(true);  // merge output and error streams
     pb.redirectInput(ProcessBuilder.Redirect.from(System.in));
     pb.redirectOutput(ProcessBuilder.Redirect.to(System.out));


     Process p = pb.start();
     int exitValue = p.waitFor();
     System.out.println("Process Completed with exit value of " + exitValue);
  } catch (java.io.IOException ex) {
  } catch (InterruptedException ex) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM