为什么我不能在Windows上直接用Java执行Cygwin .exe?

时间:2019-02-22 01:11:00

标签: java windows cygwin processbuilder

我已经安装了Cygwin,但似乎无法直接访问任何命令。例如,由于找不到bash.exe,即使我指定了它的绝对路径,也无法启动下面的过程。我知道它已正确安装,因为我可以在文件资源管理器中看到此路径。但是,搜索任何cygwin文件不会返回任何匹配的命中值。

<html prefix="og: http://ogp.me/ns#">
  <head>
    <title>The Rock (1996)</title>
    <meta property="og:title" content="The Rock" />
    <meta property="og:type" content="video.movie" />
    <meta property="og:url" content="http://www.imdb.com/title/tt0117500/" />
    <meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />
    ...
  </head>
  ....
</html>

如果将cygwin的bin添加到Windows PATH变量中,则可以通过以下方式成功执行“ ls”

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

当它在PATH中时,我仍然无法直接访问bash.exe。

我不明白这里发生了什么,为什么我不能直接执行bash.exe。

1 个答案:

答案 0 :(得分:0)

我尚未完全回答自己的问题,但发现了行之有效的方法。在Windows PATH中使用cygwin \ bin,我的进程生成器字符串为:

[cmd,/ c,C:\ cygwin64 \ bin \ sh.exe,-c,ls]

这似乎有效。我使用的是sh.exe而不是bash.exe,以便可以使用cygwin的所有bin文件。