Search code examples
javaprocessbuilder

The system cannot find the path specified when running a program from d: instead of c:


I got a folder for testing stuff on my c: disk but my main project is on my d: disk. But now i was getting the same error while the code was the same after some research i found out it was beacause of the location. But is there a fix for this ? I know i could relocate my projects but there has to be some solution.

A second thing i noticed was when i was debugging .The string inside the ProcessBuilder value only changed when i used clean(netbeans). How is this possible. I'm still new to java and want to now why this stuff behave like this.

This is my code

 public class CmdTest {

    public static void main(String[] args) throws Exception {

        ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "D: && cd program Files\\wkhtmltopdf\\bin && dir");

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

}

Solution

  • The solution was very simple.

    public class CmdTest {
    
        public static void main(String[] args) throws Exception {
    
            ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "D: && cd program Files\\wkhtmltopdf\\bin && dir");
    
            // Here you need to set the directory in my case D:
            pb=pb.directory(new File("D:\\"));
            pb.redirectErrorStream(true);
            Process p = pb.start();
            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while (true) {
                line = r.readLine();
                if (line == null) { break; }
                System.out.println(line);
            }
        }
    
    }