Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process requires redirected input

I have a UNIX native executable that requires the arguments to be fed in like this

prog.exe < foo.txt.

foo.txt has two lines: bar baz

I am using java.lang.ProcessBuilder to execute this command. Unfortunately, prog.exe will only work using the redirect from a file. Is there some way I can mimic this behavior in Java?

Of course,

ProcessBuilder pb = new ProcessBuilder("prog.exe", "bar", "baz"); 

does not work.

Thanks!

like image 942
initialZero Avatar asked Jun 03 '10 21:06

initialZero


Video Answer


2 Answers

ProcessBuilder pb = new ProcessBuilder("prog.exe");
Process p = pb.start();
OutputStream pos = p.getOutputStream();

InputStream fis = new FileInputStream("file.txt");
byte[] buffer = new byte[1024];
int read = 0;
while((read = fis.read(buffer)) != -1) {
    pos.write(buffer, 0, read);
}
fis.close();

Not tested, but something like this should work.

like image 96
Marc Avatar answered Sep 28 '22 04:09

Marc


I ended up doing something like this and it works. Thanks for all the help!

    import java.io.BufferedWriter;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.util.LinkedList;
    import java.util.List;

    public class UserInp {

        public static void main(String[] args) {
            new UserInp().sample();
        }

        public void sample() {

            String command = "foo.exe";

            List<String> args = new LinkedList<String>();

            args.add("bar");
            args.add("baz");

            ProcessBuilder pb = new ProcessBuilder(command);
            java.lang.Process process = null;

            try {
                process = pb.start();
            } catch (IOException ex) {
                //--
            }
            OutputStream os = process.getOutputStream();
            PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));

            final InputStream is = process.getInputStream();
            new Thread(new Runnable() {
                public void run() {
                    try {
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        String line;
                        while ((line = br.readLine()) != null) {
                            System.out.println(line);
                        }
                    } catch (java.io.IOException e) {
                    }
                }
            }).start();

            for (String arg : args) {
                pw.println(arg);
            }

            pw.close();

            int returnCode = -1;
            try {
                returnCode = process.waitFor();
            } catch (InterruptedException ex) {
                //--
            }
            System.out.println(returnCode);
        }
    }
like image 34
initialZero Avatar answered Sep 28 '22 06:09

initialZero