使用 warble 打包到 jar 中后,带有 Rubeus 和 Swing 的 JRuby 脚本退出

2022-01-24 00:00:00 concurrency java swing jframe jruby

我正在尝试将一个简单的 JRuby 脚本打包到一个 jar 文件中.该脚本使用 Rubeus::Swing 并在使用 JRuby 解释器执行时正确运行.

I am trying to package a simple JRuby script into a jar file. The script uses Rubeus::Swing and runs correctly when executed with the JRuby interpreter.

require 'rubygems'
require 'rubeus'

class Example01
  extend Rubeus::Swing
  def show
    JFrame.new("Rubeus Swing Example 01") do |frame|
      frame.visible = true
    end
  end
end
Example01.new.show

一旦我用 warble 将脚本打包成 JAR,当我执行时:

Once I package the script into a JAR with warble, when I execute:

java -jar jtest.jar

... JFrame 窗口出现并立即关闭.

... the JFrame window shows up and instantly closes.

没有任何错误的迹象.

有人知道为什么会这样吗?

Does anyone know why this happens?

推荐答案

Warbler 在你的主脚本退出后调用 System.exit().这会导致 Swing EventThread 退出,关闭您的应用程序.

Warbler calls System.exit() after your main script exits. This causes the Swing EventThread to exit, closing your app.

https://github.com/jruby/warbler/blob/master/ext/JarMain.java#L131

我通过加入启动脚本底部的事件线程来解决这个问题,如下所示:

I worked around this problem by joining with the event thread at the bottom of my start script like so:

event_thread = nil
SwingUtilities.invokeAndWait { event_thread = java.lang.Thread.currentThread }
event_thread.join

Hacky,但它确实有效.

Hacky, but it works.

相关文章