【发布时间】:2016-12-25 18:16:57
【问题描述】:
我在 test.rb 中有以下代码:
require 'open3'
cmd = 'C:\Program Files\foo\bar.exe'
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
puts "stdout: #{stdout.read}"
puts "\n\n"
puts "stderr: #{stderr.read}"
end
bar.exe 是我创建的控制台应用程序,位于C:\Program Files\foo\。当我运行bar.exe:
- 它输出
"Hello world!" - 使用任何参数,例如
bar.exe /blah,它会输出帮助消息。
当我运行ruby test.rb 时,我得到了这个错误:
C:\RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/open3.rb:193:in 'spawn': No such file or directory - C:\Program Files\foo\bar.exe (Errno::ENOENT)
from C:\RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/open3.rb:193:in 'popen_run'
from C:\RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/open3.rb:193:in 'popen3'
from test.rb:3:in '<main>'
如果我将代码更改为调用popen3:
Open3.popen3(cmd, '')
我没有收到 Errno::ENOENT 错误,而是收到帮助消息,但我想要 "Hello World" 输出。
我搜索了一个解决方案,但没有任何效果,包括“Why does Open3.popen3 return wrong error when executable is missing?”的答案。
为什么会出现此错误以及如何解决?
【问题讨论】:
-
'C:/Program Files/foo/bar.exe'可以替代吗?