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

Difference between revisions of "JRuby/SWTSetup"

Line 2: Line 2:
 
* download [http://jruby.codehaus.org JRuby]
 
* download [http://jruby.codehaus.org 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)
 
* 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 [http://www.eclipse.org/swt SWT]
+
* download the standalone [http://www.eclipse.org/swt SWT] (Mac users may grab either the Cocoa or Carbon version of SWT, but you know you want Cocoa)
 
* extract it somewhere else
 
* extract it somewhere else
 
* copy swt.jar to the jruby lib directory, for example: /usr/lib/jruby/lib
 
* copy swt.jar to the jruby lib directory, for example: /usr/lib/jruby/lib
Line 27: Line 27:
 
</pre>
 
</pre>
  
* 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:
+
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:
 
<pre>
 
<pre>
 
irb(main):001:0> display = org.eclipse.swt.widgets.Display.default
 
irb(main):001:0> display = org.eclipse.swt.widgets.Display.default
Line 36: Line 36:
 
</pre>
 
</pre>
  
* to spin the SWT event loop:
+
To spin the SWT event loop:
 
<pre>
 
<pre>
 
irb(main):006:0> display.sleep unless display.readAndDispatch until shell.isDisposed
 
irb(main):006:0> display.sleep unless display.readAndDispatch until shell.isDisposed
 
</pre>
 
</pre>
  
* to exit:
+
To exit jirb:
 
<pre>
 
<pre>
 
irb(main):007:0> exit
 
irb(main):007:0> exit
 
</pre>
 
</pre>
  
* 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:
+
Note that when running on the Mac, the SWT event loop must run on the first thread of the process, otherwise you'll get the spinning beachball and the app will be unresponsive (and will have to be killed with Force Quit).  This is also true for jirb.  To do this, specify the -XstartOnFirstThread=true JVM argument as follows:
 
<pre>
 
<pre>
 
jruby -J-XstartOnFirstThread Snippet1.rb
 
jruby -J-XstartOnFirstThread Snippet1.rb

Revision as of 01:36, 28 July 2009

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 (Mac users may grab either the Cocoa or Carbon version of SWT, but you know you want Cocoa)
  • 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 jirb:

irb(main):007:0> exit

Note that when running on the Mac, the SWT event loop must run on the first thread of the process, otherwise you'll get the spinning beachball and the app will be unresponsive (and will have to be killed with Force Quit). 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