ProcessBuilder can't find file?!

前端 未结 2 789
名媛妹妹
名媛妹妹 2020-12-19 01:44

Another question in quick succession but this has to be a really obvious error that I am not seeing. I\'ve written some code to run a batch file below but I\'m getting an er

相关标签:
2条回答
  • 2020-12-19 02:16

    I'm running Linux, but the same error occurs when I run your code (modified to run a .sh rather than .bat).

    Try:

    ProcessBuilder pb = new ProcessBuilder("c:\\adb.bat");
    

    Apparently using ProcessBuilder.directory doesn't affect the working directory (for the purposes of discovering the executable) that was chosen when the builder was constructed (at least, that's what seems to happen. The docs say it will change the working directory, so I guess input/output files might be relative to that?)

    I'm not sure what it's actually doing internally, but providing the path to the executable in the constructor fixed the problem.

    This post talks about the problem and this solution, but also raises whether environment variables have to be set, of which "path"-like variables might be useful to help ProcessBuilder discover an executable.

    0 讨论(0)
  • 2020-12-19 02:38

    Hi try to use the tutorial here - http://www.javabeat.net/examples/2007/08/21/using-the-new-process-builder-class/. Using it I have changed your class a bit and it finds the file (note that I don't know what is inside so can't fully test it). It compiles and runs without problem, while your own I experience same problems as you:

    public class Pull {
    
    
    public void pullData() throws IOException {
        /*ProcessBuilder pb = new ProcessBuilder("adb.bat");
        File f = new File("C:\\");
        pb.directory(f);
        Process p = pb.start(); 
        */
        ProcessBuilder p = new ProcessBuilder("C:\\adb.bat");
         p.start();
        System.out.println(p.toString());
    }
    
    
     public static void main(String[] args) throws IOException {
    
    
         Pull pull = new Pull();
         pull.pullData();
    
     }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题