简体   繁体   English

Groovy为什么即使在我的PATH上也找不到该程序?

[英]Why can't Groovy find this program even though it's on my PATH?

tl;dr: Groovy cannot find a program ( phantomjs , which is on my $PATH ) when I attempt to execute the shell command from a class -- it's otherwise finds it just fine from the Groovy Console or from a Grails CLI script. tl; dr:当我尝试从一个类执行shell命令时,Groovy找不到程序( phantomjs ,位于我的$PATH ),否则它会从Groovy Console或Grails CLI脚本中找到它。 What gives? 是什么赋予了?

We have a Grails script to execute JavaScript unit tests in PhantomJS ("the headless WebKit"). 我们有一个Grails脚本,可以在PhantomJS(“无头WebKit”)中执行JavaScript单元测试。 When these tests are executed in a stand-alone script (call it grails test-js ) things work just fine. 当这些测试在独立脚本(称为grails test-js )中执行时,一切正常。 The relevant line from the Grails/Gant script was: Grails / Gant脚本中的相关行是:

// where 'jsTests' is a List with the paths to each test
def failures = 0
jsTests.each {
  Process p = "phantomjs lib/phantom-jasmine/lib/run_jasmine_test.coffee file://${it}".execute()
  failures += p.exitValue()
}

But since we want this to run as part of the usual grails test-app cycle, we created an implementation of org.codehaus.groovy.grails.test.GrailsTestType . 但是,由于我们希望此代码作为常规grails test-app周期的一部分运行,因此我们创建了org.codehaus.groovy.grails.test.GrailsTestType的实现。 When we get to the part in that implementation where we need to execute the tests, that same code (as above) doesn't work, and Groovy/Grails complains that: 当我们到达实现中需要执行测试的部分时,相同的代码(如上所述)不起作用,Groovy / Grails抱怨说:

java.io.IOException: Cannot run program "phantomjs": error=2, No such file or directory

My first thought was that phantomjs was not on my $PATH -- but I know that it is, and again: it worked when run from the Grails/Gant script. 我首先想到的是phantomjs不在我的$PATH -但我知道它确实存在,并且再次:从Grails / Gant脚本运行时,它可以工作。 So I tried it in the Groovy Console, and it works OK from there. 所以我在Groovy Console中尝试了它,然后从那里开始就可以了。

Now, if I switch from phantomjs to /absolute/path/to/phantomjs , then it works fine. 现在,如果我从phantomjs切换到/absolute/path/to/phantomjs ,那么它可以正常工作。 But hard-coding an absolute filesystem path into the class cannot be the solution. 但是,将绝对文件系统路径硬编码到类中并不是解决方案。

So: why isn't Groovy finding phantomjs under those circumstances? 那么:为什么Groovy在这种情况下不能找到phantomjs

Update: I suspect that this might have something to do with my IDE (IntelliJ Idea), as this error doesn't appear to be happening when running this from the command line. 更新:我怀疑这可能与我的IDE(IntelliJ Idea)有关,因为从命令行运行此错误似乎没有发生此错误。

As it turns out, this had everything to do with trying to run the tests from within my IDE (IntelliJ IDEA) and, more specifically, trying to do this on an OS X machine. 事实证明,这与尝试从我的IDE(IntelliJ IDEA)内运行测试,尤其是尝试在OS X机器上运行测试有关。

The solution was to add phantomjs to my PATH using launchd.conf . 解决方案是使用launchd.confphantomjs添加到我的PATH中。 I... 一世...

  1. created /etc/launchd.conf 创建了/etc/launchd.conf
  2. added the following line to it: (note: I installed PhantomJS with Homebrew ) 向其添加了以下行:(注意:我用Homebrew安装了PhantomJS)

    setenv PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

  3. restarted my machine so that the new launchd config would take effect. 重新启动我的机器,以便新launchd配置生效。
  4. went back into my IDE and re-ran the tests. 回到我的IDE,然后重新运行测试。

And all the tests ran successfully. 并且所有测试均成功运行。

You can try printing your path with groovy 您可以尝试使用groovy打印路径

['sh', '-c', 'echo $PATH' ].execute().text

or 要么

println System.getenv('PATH')

You can to create an environment variable with absolute path to phantomjs and verify it in runtime and to alert if it is not properly configured. 您可以创建一个具有phantomjs绝对路径的环境变量,并在运行时对其进行验证,并在未正确配置的情况下发出警报。

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

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