简体   繁体   English

Java ProcessBuilder + bash:“没有这样的文件或目录”

[英]Java ProcessBuilder + bash: “No such file or directory”

I'm trying to run bash from Java on Windows (here with the Windows Linux Subsystem, but Git Bash is the same), but even the basics are failing: 我正在尝试在Windows上从Java运行bash(在此使用Windows Linux子系统,但是Git Bash是相同的),但是即使基本操作也失败了:

bash --noprofile --norc -c 'echo $PWD'`

In cmd.exe this works fine: cmd.exe可以正常工作:

在CMD.exe中工作正常

In java: 在Java中:

import static java.util.stream.Collectors.joining;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;

public class ProcessBuilderTest {
    public static int runBatch() {
        List<String> commandLine = new ArrayList<>();
        // both following lines have the same result
        commandLine.add("bash");
        // commandLine.add("C:\\Windows\\System32\\bash.exe"); 

        commandLine.add("--noprofile");
        commandLine.add("--norc");
        commandLine.add("-c");
        commandLine.add("'echo $PWD'");

        System.out.println("cmd: " + commandLine.stream().collect(joining(" ")));
        try {
            ProcessBuilder processBuilder = new ProcessBuilder(commandLine);
            Process process = processBuilder
                .redirectErrorStream(true)
                .start();
            new BufferedReader(new InputStreamReader(process.getInputStream())).lines()
                .forEach(System.out::println);
            return process.waitFor();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        } catch (InterruptedException e) { // NOSONAR
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        runBatch();
    }
}

Running the above results in the following, and the process never exits. 运行上述结果将导致以下结果,并且该过程永不退出。

cmd: bash --noprofile --norc -c 'echo $PWD'
/bin/bash: echo /mnt/c/scratch/pbtest: No such file or directory

Expected behavoir: no error and the process terminates. 预期的行为:没有错误,过程终止。

bash is running in cmd.exe in your windows environment bash在Windows环境中的cmd.exe中运行

Could you have java running the following command instead ? 您可以让Java运行以下命令吗?

cmd.exe /c bash --noprofile --norc -c 'echo $PWD'

ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", 
    "/c", 
    "bash --noprofile --norc -c 'echo $PWD'");

or with the List as you initially tried 或您最初尝试使用的列表

Inspired by mkyong post 灵感来自mkyong post

Using Git-bash: I changed these two lines and ir ran as expected: 使用Git-bash:我更改了这两行,ir的运行符合预期:

// Used full path to the underlying bash.exe shell
commandLine.add("\"C:\\Program Files\\Git\\bin\\bash.exe\"");

// Removed quotes
commandLine.add("echo $PWD"); 

Ref: https://superuser.com/a/1321240/795145 参考: https : //superuser.com/a/1321240/795145

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

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