ANN: rjava

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

Hi Hans,

Everything installed OK on my Windows 2000 box here at work, but when I run any of the samples, I get the following error:

d:/usr/lib/ruby/site_ruby/1.8/java.rb:29:in fork': The fork() function is unimp lemented on this machine (NotImplementedError) from d:/usr/lib/ruby/site_ruby/1.8/java.rb:29:instart_tcp’
from hello.rb:5

Have you tried rjava on Windows? Ie, does it currently only work on Unix (in which case, I’ll play with it at home), or should I persevere trying to work out why Ruby’s fork() doesn’t work on Windows (maybe it never has, and I’ve just never tried it before)?

Cheers,

Harry O.

Hans Jörg Hessmann wrote:

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!”)

Sounds sweet (though I’ve noticed it is currently only for non-Windows).

One request, though: I noticed that there is a file simply named
‘java.rb’ that is installed in the root of site_ruby/1.8. Since this is
a pretty generic file name, and others may write Java-related Ruby libs,
the file should either be renamed or placed in its own subdirectory to
avoid any possible conflicts.

Thanks,

James

Hi Hans,

Everything installed OK on my Windows 2000 box here at work, but when I
run any of the samples, I get the following error:

d:/usr/lib/ruby/site_ruby/1.8/java.rb:29:in fork': The fork() function is unimp lemented on this machine (NotImplementedError) from d:/usr/lib/ruby/site_ruby/1.8/java.rb:29:in start_tcp’
from hello.rb:5

Have you tried rjava on Windows? Ie, does it currently only work on
Unix (in which case, I’ll play with it at home), or should I persevere
trying to work out why Ruby’s fork() doesn’t work on Windows (maybe it
never has, and I’ve just never tried it before)?

Fork is a unix system call which is not implemented in Windows. It is
implemented in Cygwin. I doubt there’s an easy workaround.

Cheers,

Harry O.

Gavin

This presents an opportunity for me to propose that things installed in
site-ruby/1.8 be placed in a subdirectory with either a registered name
(probably registered at ruby-lang.org) or a name guaranteed to be
unique. Mac OS X uses URI’s as names guaranteed to be unique and avoids
collisions with programs that might confuse the name with an actual URI
by reversing the URI (i.e, apple.com becomes com.apple). Those without
their own domains might use permanent e-mail addresses (i.e.,
net.isp@username) or, if the RubyForge people, as an example, implement
it, RubyForge usernames (i.e, org.rubyforge.username), or something
similar. This could avoid massive problems in the not too distant
future.

Regards,

Mark

···

On Sunday, October 19, 2003, at 09:20 PM, James Britt wrote:

Hans Jörg Hessmann wrote:

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!”)

Sounds sweet (though I’ve noticed it is currently only for
non-Windows).

One request, though: I noticed that there is a file simply named
‘java.rb’ that is installed in the root of site_ruby/1.8. Since this
is a pretty generic file name, and others may write Java-related Ruby
libs, the file should either be renamed or placed in its own
subdirectory to avoid any possible conflicts.

Have you tried rjava on Windows? Ie, does it currently only work on Unix
(in which case, I’ll play with it at home), or should I persevere trying to
work out why Ruby’s fork() doesn’t work on Windows (maybe it never has, and
I’ve just never tried it before)?

Well, I don’t have Windows installed at home. The fork() is used to start the
JVM in the background. As workaround you can start the JVM manually:

java -jar rjava/rjava.jar -verbose

Then you have to replace JVM.start_tcp() by JVM.connect():

require "java"
include JAVA
JVM.connect().import("java.lang.System").out.println("Hello World!")

This should work on Windows, but I haven’t tried it.

Hans Jörg

One request, though: I noticed that there is a file simply named
‘java.rb’ that is installed in the root of site_ruby/1.8. Since this is
a pretty generic file name, and others may write Java-related Ruby libs,
the file should either be renamed or placed in its own subdirectory to
avoid any possible conflicts.

Well, I thought this is the most intuitive way to use it. As compromise I’ll
move the java.rb to the rjava directory for the next release.

Hans Jörg

Gavin Sinclair wrote:

Fork is a unix system call which is not implemented in Windows.

I knew that much :-). I just didn’t know whether Ruby “faked” it on Windows, as it does with its own threading.

It is
implemented in Cygwin. I doubt there’s an easy workaround.

The easiest workaround I could imagine would be to make the library a client for a separate server. Not the neatest thing, but at least it would work.

That is probably do-able, since (if I read the doco for rjava correctly) it uses TCP/IP to talk to the JVM.

Anyway, I’m not overly fussed, since I’m more likely to use something like rjava on Linux or Solaris. I just happen to sit in front of one of Bill’s boxes here at work.

Harry O.