ANN: rjava

  • Thread starter Hans Jörg Hessmann
  • Start date
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
 
H

Harry Ohlsen

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)?

Cheers,

Harry O.
 
G

Gavin Sinclair

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
 
H

Harry Ohlsen

Gavin said:
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.
 
J

James Britt

Hans said:
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
 
H

Hans Jörg Hessmann

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
 
H

Hans Jörg Hessmann

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,138
Messages
2,570,803
Members
47,349
Latest member
jojonoy597

Latest Threads

Top