Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

JRuby/SWTSetup

To set up JRuby with SWT:

  • download JRuby
  • extract it somewhere, for example: c:\jruby (on Windows), ~/jruby (on Linux or Mac), or /usr/lib/jruby (on Linux or Mac, but this will likely require admin privileges)
  • download the standalone SWT
  • extract it somewhere else
  • copy swt.jar to the jruby lib directory, for example: /usr/lib/jruby/lib
  • add the jruby bin directory to your path, for example:
    • on Windows: set PATH=c:\jruby\bin;%PATH%
    • on Linux or Mac: export PATH=/usr/lib/jruby/bin:$PATH
  • ensure jruby runs:
jruby -v

should produce something like:

jruby 1.3.1 (ruby 1.8.6p287) (2009-06-15 2fd6c3d) (Java HotSpot(TM) Client VM 1.5.0_16) [i386-java]
  • try some SWT expressions interactively in jirb:
jirb
irb(main):001:0> display = org.eclipse.swt.widgets.Display.getDefault
irb(main):002:0> shell = org.eclipse.swt.widgets.Shell.new(display)
irb(main):003:0> shell.setText("Hi there")                           
irb(main):004:0> shell.open                                          
irb(main):005:0> shell.dispose
  • note that JRuby lets you write using Java bean conventions, for example: shell.text="foo" is equivalent to shell.setText("foo"), and Display.default is equivalent to Display.default, so the above could also be written:

irb(main):001:0> display = org.eclipse.swt.widgets.Display.default irb(main):002:0> shell = org.eclipse.swt.widgets.Shell.new(display) irb(main):003:0> shell.text="Hi there" irb(main):004:0> shell.open irb(main):005:0> shell.dispose


  • to spin the SWT event loop:
irb(main):006:0> display.sleep unless display.readAndDispatch until shell.isDisposed
  • to exit:
irb(main):007:0> exit
  • Note that when running on the Mac, the SWT event loop must run on the first thread of the JRuby process, otherwise you'll get the spinning beachball and the app will be unresponsive. This is also true for jirb. To do this, specify the -XstartOnFirstThread=true JVM argument as follows:
jruby -J-XstartOnFirstThread Snippet1.rb

or you can do:

export JAVA_OPTS=-XstartOnFirstThread
jruby Snippet1.rb

Unfortunately, jirb does not recognize the -J option, so only the latter method works with jirb.

Back to the top