Java processbuilder run cygiwn commands

997 views Asked by At

Trying to write code to run cygwin commands from java code using processbuilder. not sure what's wrong. Please help me, Thank in Advanced!!

following code

import java.io.*;

public class test_6 {
    public static void main(String[] args) throws Exception {
        ProcessBuilder buildercmd = new ProcessBuilder(
            "cmd.exe", "/c", "cd \"D:\\csi\" && dir" +
                    "&& dir");

        ProcessBuilder buildercygwin = new ProcessBuilder(
                "D:/app/Cygwin/bin/bash", "-c", "cd /cygdrive/D/csi/ && dir" +
                        "&& ls -l");

        buildercygwin.redirectErrorStream(true);
        Process p = buildercygwin.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) { break; }
            System.out.println(line);
        }
    }
}

it worked using cmd as you see on my code but some reason its not working using cygwin.

following output i am getting:

/usr/bin/bash: dir: command not found
1

There are 1 answers

2
codemonk113 On BEST ANSWER

try using the full path to dir => D:/app/Cygwin/bin/dir

cygwin has a dir.exe, for the windows version it is an internal commmand. source:

https://superuser.com/questions/229945/where-are-the-standard-windows-prompt-commands-files