简体   繁体   中英

give buffered input to runtime exec in java

This is my code for run a java file in other java application but i dont knoow what to do if the program takes only bufferedinputs ??

try {

Runtime rt = Runtime.getRuntime();

// compile the java file
Process pr = rt.exec("javac Main.java");
pr.waitFor();

// run the java file
pr = rt.exec("java Main " + inputs.toString()); // using this i can give command line arguments 
pr.waitFor();
}

This is my code i can give command line arguments at run time but what if i want to give bufferedinput to the program ?

Thanks in advance

You state:

This is my code for run a java file in other java application but i dont knoow what to do if the program takes only bufferedinputs ??

To attach to another processes input and output streams, look at the API for the Process class where you'll find and use the getErrorStream() , getInputStream() and getOutputStream() methods. You can then wrap your Input and Output and Error Streams in their respective Buffered Streams.

Note however that you should be wary of common pitfalls which are well explained in the slightly dated article, When Runtime Exec Won't

Having said this, you're far better off using the Java classes themsevels rather than running it in another JVM. Is there a reason that you can't do this? And what do you mean by "buffered" input?

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