java - 我需要在 Java 的特定工作目录中执行命令行程序

标签 java command-line runtime execute

我需要执行一个带有 2 个参数的命令行程序。但是,它必须在工作目录中执行。即“命令 arg1 arg2”,而不是“c:\folder\subfolder\command arg1 arg2”

从这里的其他问题,我开始使用 Runtime.exec(cmdArray, null, workingDirectory);但我不断收到“CreateProcess error=2,系统找不到指定的文件”。我查过了,路径和文件都存在,所以我不知道哪里出了问题。这是我正在使用的代码。

String [] fileName = {"mp3wrap.exe", "Clear_10", "*.mp3"};
String dirName = "E:\\Music\\New Folder\\zz Concatinate\\Clear_10";
try {
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(fileName, null, new File(dirName));
    BufferedReader input = new BufferedReader(new InputStreamReader
        (pr.getInputStream()));
    String line = null;
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }//end while
    int exitVal = pr.waitFor();
    System.out.println("Exited with error code " + exitVal);
}//end try
catch (Exception e) {
    System.out.println(e.toString());
    e.printStackTrace();
}//end catch`

我收到这个错误:

java.io.IOException: Cannot run program "mp3wrap.exe"(in directory "E:\Music\New Folder\zz Concatinate\Clear_10"): CreateProcess error=2, 系统找不到指定的文件

最佳答案

给出 mp3wrap.exe 的完整路径。

Java 不使用 PATH 来查找 mp3wrap。

-- 评论后更新:

好的 - 重读问题,他问如何从目录中启动程序。如果程序需要它,您必须在该目录中启动 Java 程序。

您可能仍然需要提供程序的完整路径,或者启动它并指示在当前目录中搜索它。我记得,在 Windows 中,总是搜索当前目录。其他系统在这里有所不同,因此您可以用点表示当前目录,这在 Windows 上也适用:“./mp3wrap”。

关于java - 我需要在 Java 的特定工作目录中执行命令行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10576205/

相关文章:

c++ - 在模板结构的所有实例中将成员设置为相同的值

c++ - 运行时函数在编译时函数上分支?

java - 如何在 Android 中使用小空格作为数字分隔符?

java - Blackberry:如何在录制前设置视频分辨率

java - 是否存在提供任意深度嵌套以转换为 JSON 对象的 Java 类?

bash - 使用 `alias' 添加默认选项 - 例如 `alias cd="cd -P"'

mysql shell无法连接到mysql服务器

java - 确保字符串具有特定的模式

bash - 如何扩展 bash shell?

java - 在命令提示符下运行时执行期间无法找到文件