Ruby 1.9 Kernel::system 无法在 Windows 上运行

发布于 2024-09-26 21:51:01 字数 138 浏览 2 评论 0原文

我在 ruby​​ 脚本中使用 system "java -cp xxx.jar" ,它在 Mac OS 上运行良好。但是当我在Windows 7 x64上运行该脚本时,那些java -cp xxx.jar没有得到执行,也没有报告错误。

I use system "java -cp xxx.jar" in my ruby script and it runs well on Mac OS. But when I run the script on Windows 7 x64, those java -cp xxx.jar did not get executed and no error was reported.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分分钟 2024-10-03 21:51:01

如果您的命令无法运行,system 不会抛出异常或任何异常(这可能就是您说“没有报告错误”的原因)。

因此,您需要检查 java 是否在您的 PATH 中;默认情况下,在 Windows 上不是这样,您需要将 JDK 的 bin 目录添加到您的 PATH 中。

system doesn't throw an exception or anything if your command fails to run (which may be why you said "no error was reported").

So, you need to check whether java is in your PATH; by default on Windows, it's not, and you need to add the JDK's bin directory to your PATH.

孤独陪着我 2024-10-03 21:51:01

另外,如果您在类路径中使用多个 java 类,并且在 Windows 情况下使用“:”(冒号)而不是“;”(分号),则您的脚本也无法执行;

classpath_separator = RUBY_PLATFORM =~ /mswin/ ? ';' : ':'

如果你想捕获系统命令的输出,你可以使用下面的代码:

def run_cmd cmd, cmd_name = 'Command'
# save current STDOUT reference
default_stdout = STDOUT.dup
# temp file used to capture output of the child processes
# and avoid conflicts between several processes running at the same time
# (temp file has a unique name and will be cleaned after close)
tmp_file = Tempfile.new 'tmp'

cmd_output = ''
puts "Begin #{cmd_name}: #{cmd}"
begin
  # redirect default STDOUT to the tempfile
  $stdout.reopen tmp_file
  # execute command
  system "#{cmd} 2>&1"
ensure
  # read temp file content
  tmp_file.rewind
  cmd_output = tmp_file.read
  tmp_file.close
  # restore default STDOUT
  $stdout.reopen default_stdout
end

# push output to console
puts "Output of #{cmd_name}: #{cmd_output}"
puts "End #{cmd_name}"

cmd_output
end

Also your script can not be executed if you use several java classes in the classpath with ":"(colon) instead of ";"(semicolon) in Windows case;

classpath_separator = RUBY_PLATFORM =~ /mswin/ ? ';' : ':'

And if you want to catch output of the system command you can use next code:

def run_cmd cmd, cmd_name = 'Command'
# save current STDOUT reference
default_stdout = STDOUT.dup
# temp file used to capture output of the child processes
# and avoid conflicts between several processes running at the same time
# (temp file has a unique name and will be cleaned after close)
tmp_file = Tempfile.new 'tmp'

cmd_output = ''
puts "Begin #{cmd_name}: #{cmd}"
begin
  # redirect default STDOUT to the tempfile
  $stdout.reopen tmp_file
  # execute command
  system "#{cmd} 2>&1"
ensure
  # read temp file content
  tmp_file.rewind
  cmd_output = tmp_file.read
  tmp_file.close
  # restore default STDOUT
  $stdout.reopen default_stdout
end

# push output to console
puts "Output of #{cmd_name}: #{cmd_output}"
puts "End #{cmd_name}"

cmd_output
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文