[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ANN: rjava

Hans Jörg Hessmann

10/19/2003 11:35:00 AM

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....

Hans Jörg Hessmann


6 Answers

Harry Ohlsen

10/19/2003 11:27:00 PM

0

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.



Gavin Sinclair

10/20/2003 12:55:00 AM

0

> 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



Harry Ohlsen

10/20/2003 1:04:00 AM

0

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.



James Britt

10/20/2003 1:21:00 AM

0

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



Hans Jörg Hessmann

10/20/2003 10:27:00 AM

0

> 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


Hans Jörg Hessmann

10/20/2003 10:39:00 AM

0

> 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