Java wrapper around a PE (.exe)
Is there any way to make a Java program (in Windows) that just acts as a wrapper around a PE (.exe), passing all stdin input to the program and writing out to stdout everything that the PE writes out.
I need this because the interface for a program only allows 开发者_C百科Java classes, but I want it to run some code that I've put together in C++.
Thanks in advance.
edit: portability is 0% important. This only needs to work in Windows and will never be needed to work anywhere else.
Take a look at ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory("myDir");
Process p = pb.start();
and another example of it.
Yes this is possible with java.lang.Runtime.ecec() and java.lang.Process with which you can acces all 3 streams(in/out/err) to the *.exe you are executing.
Others have mentioned the standard Java mechanisms (ProcessBuilder and its ilk). However, reliably re-routing stdout/stdin/errout requires care (additional). If you don't need to process this I/O inside your Java app, consider using a native call (e.g. C's system
function) via JNI or JNA (demo here).
精彩评论