问题
I've installed Cygwin but can't seem to access any commands directly. For example, the process below doesn't start due to bash.exe not found even though I'm specifying it's absolute path. I know it's installed correctly since I can see this path in the File Explorer. However, searching for any cygwin file doesn't return any hits which is odd.
ProcessBuilder pb = new ProcessBuilder("C:\\cygwin64\\bin\\bash.exe", "-c", "ls");
Process p = pb.start();
java.io.IOException: Cannot run program "C:\cygwin64\bin\bash.exe" (in directory "C:\Users\tyea1\Documents\Log Bundles\latest_PRODXIO01"): CreateProcess error=2, The system cannot find the file specified
If I add cygwin's bin to the Windows PATH variable, I can successfully execute "ls" via
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "ls");
Process p = pb.start();
I still can't access bash.exe directly when it's in the PATH.
I don't understand what is going on here and why I can't execute bash.exe directly.
回答1:
I haven't exactly answered my own question but found something that works. With the cygwin \bin in the Windows PATH, my process builder string is:
[cmd, /c, C:\cygwin64\bin\sh.exe, -c, ls]
This seems to be working. I'm using sh.exe instead of bash.exe so that I can use all of cygwin's bin files.
来源:https://stackoverflow.com/questions/54818613/why-cant-i-execute-a-cygwin-exe-directly-with-java-on-windows