ruby 反引号,kernel.system,exec ,命令不成功
我正在尝试从我的 ruby 代码执行 java 程序。
我的代码类似于:
cmd = "java -cp \"path/lib/*\" com.blah.blah"
# %x[#{cmd}]
# `#{cmd}`
# Kernel.system(cmd)
Kernel.exec(cmd)
我的问题是,该命令以任何一种方式都无法成功运行。我知道 %x, backticks 在子 shell 中运行,因此可能缺少一些环境变量。 不过我希望 Kernel.system、Kernel.exec 能够成功。
如果我直接在 shell 中输入该命令,则该命令运行良好。调试这个有什么帮助吗?
谢谢!
I am trying to execute a java program form my ruby code.
My code is something like:
cmd = "java -cp \"path/lib/*\" com.blah.blah"
# %x[#{cmd}]
# `#{cmd}`
# Kernel.system(cmd)
Kernel.exec(cmd)
My problem, is the command does not run successfully in either way. I know that %x, backticks runs in a subshell so probably some env variables are missing.
However I was hoping for Kernel.system, Kernel.exec to succeed.
The command runs fine if I directly type it in a shell. Any help in debugging this ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,问题是我没有关闭我正在创建的文件。
显然,当我直接从 shell 或直接从 .sh 调用它时,这很好,因为 ruby 脚本已经退出。
然而,当我从 Ruby 脚本调用它时,它将无法访问该文件并会产生问题。
底线:关闭文件,否则会浪费半天的工作时间!
Turns out the issue was I was not closing the files I was creating.
Apparently that was fine when I was invoking it directly from the shell or directly from .sh, because the ruby script had already exited.
However when I would invoke this from the Ruby script it would not be able to access the file and would create issues.
Bottom line: Close your files else loose half day of work !