简体   繁体   English

从java程序执行bat文件时找不到文件

[英]File not found when execution a bat file from a java program

I have this code that used to work, but now I´m getting that the program can´t find the file but the file is there.我有这个曾经可以工作的代码,但现在我发现程序找不到文件,但文件在那里。

This is my code.这是我的代码。 My project is in C:\\Users\\user\\git\\project and the apps folder is C:\\Users\\user\\git\\apps\\folder .我的项目在C:\\Users\\user\\git\\project ,apps 文件夹是C:\\Users\\user\\git\\apps\\folder

        ProcessBuilder pb = null;
       pb = new ProcessBuilder("myApp.bat");                         
        pb.directory(newFile("C:\Users\user\git\project\..\apps\folder"));
        File log = new File("log");
        pb.redirectErrorStream(true);
        pb.redirectOutput(Redirect.appendTo(log));

        Process p = pb.start();
      assert pb.redirectInput() == Redirect.PIPE;
      assert pb.redirectOutput().file() == log;
      assert p.getInputStream().read() == -1;

And I´m getting Cannot run program "myApp.bat" (in directory "C:\\Users\\user\\git\\project\\..\\apps\\folder"): CreateProcess error=2, Can´t find file我得到了Cannot run program "myApp.bat" (in directory "C:\\Users\\user\\git\\project\\..\\apps\\folder"): CreateProcess error=2, Can´t find file

You are specifying an incorrect folder path for your bat file.您为 bat 文件指定了错误的文件夹路径。

You are specifying您正在指定

C:\\Users\\user\\git\\project\\..\\apps\\folder

but you are saying that the correct folder is C:\\Users\\user\\git\\project\\..\\apps\\folder .但你说正确的文件夹是C:\\Users\\user\\git\\project\\..\\apps\\folder

Change the line换线

pb.directory(newFile("C:\\Users\\user\\git\\project\\..\\apps\\folder"));

to

pb.directory(newFile("C:\\Users\\user\\git\\project\\apps\\folder"));

The .. refers to change in directory. ..指的是目录中的更改。 So your actual path becomes C:\\Users\\user\\git\\apps\\folder instead of C:\\Users\\user\\git\\project\\apps\\folder所以你的实际路径变成 C:\\Users\\user\\git\\apps\\folder 而不是 C:\\Users\\user\\git\\project\\apps\\folder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM