H
Hans Jörg Hessmann
RJava enables you to use Java classes from ruby using ruby-like syntax. For
example:
require "java"
include JAVA
JVM.start_tcp().import("java.lang.System").out.println("Hello World!")
lets the Java Virutal Machine print “Hello World!†on its console. A little
more complex example shows most abilities of the current implementation:
require "java"
include JAVA
def create_pane
pane = @jvm.new("javax.swing.JPanel", @jvm.new("java.awt.GridBagLayout"))
gbc = @jvm.new("java.awt.GridBagConstraints")
label = @jvm.new("javax.swing.JLabel", "Some Text")
gbc.fill = gbc.jclass.BOTH
pane.add(label, gbc)
pane
end
@jvm = JVM.start_tcp
jframe_class = @jvm.import("javax.swing.JFrame")
jframe = jframe_class.new("Ruby-Swing-Sample")
jframe.setSize(300, 200)
jframe.setDefaultCloseOperation(jframe_class.DISPOSE_ON_CLOSE)
jframe.setContentPane(create_pane)
jframe.setVisible(true)
It opens a Swing frame with a label on it.
The current implementation is an early preview with several limitations:
- The only supported communication channel between Java and Ruby is TCP/IP.
Other channels using a pipe or JNI are planned for future releases.
- The communication is one-way from Ruby to Java. The next major release will
allow call-backs from Java to Ruby by implementing Java interfaces with ruby
classes.
- The Java objects that were referenced by ruby are never collected by the
Java garbage collector. This will be fixed in the next major release.
- Tests, samples and documentation have to be added.
- Performance has to be optimized
If you are interested, get it from:
http://www.spricom.com/rjava/
Hans Jörg Hessmann
example:
require "java"
include JAVA
JVM.start_tcp().import("java.lang.System").out.println("Hello World!")
lets the Java Virutal Machine print “Hello World!†on its console. A little
more complex example shows most abilities of the current implementation:
require "java"
include JAVA
def create_pane
pane = @jvm.new("javax.swing.JPanel", @jvm.new("java.awt.GridBagLayout"))
gbc = @jvm.new("java.awt.GridBagConstraints")
label = @jvm.new("javax.swing.JLabel", "Some Text")
gbc.fill = gbc.jclass.BOTH
pane.add(label, gbc)
pane
end
@jvm = JVM.start_tcp
jframe_class = @jvm.import("javax.swing.JFrame")
jframe = jframe_class.new("Ruby-Swing-Sample")
jframe.setSize(300, 200)
jframe.setDefaultCloseOperation(jframe_class.DISPOSE_ON_CLOSE)
jframe.setContentPane(create_pane)
jframe.setVisible(true)
It opens a Swing frame with a label on it.
The current implementation is an early preview with several limitations:
- The only supported communication channel between Java and Ruby is TCP/IP.
Other channels using a pipe or JNI are planned for future releases.
- The communication is one-way from Ruby to Java. The next major release will
allow call-backs from Java to Ruby by implementing Java interfaces with ruby
classes.
- The Java objects that were referenced by ruby are never collected by the
Java garbage collector. This will be fixed in the next major release.
- Tests, samples and documentation have to be added.
- Performance has to be optimized
If you are interested, get it from:
http://www.spricom.com/rjava/
Hans Jörg Hessmann