tags:

views:

122

answers:

3

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 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.

+2  A: 

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.

x4u
+5  A: 

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.

TofuBeer
Cool. I'll try that and get back to you (or tick this :)
Peter Alexander
+1  A: 

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).

McDowell