[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby and Java

Wayne Pierce

6/21/2005 1:12:00 PM

I have a vendor product with Java APIs that I need to write against
and would like to use Ruby.

Does anyone have an opinion on the most reliable way to interact with
Java from Ruby? I've seen some different projects, but can't tell if
there is any major difference between how they are implemented or
which are actively maintained.

Thanks,

Wayne


9 Answers

Jean-Claude Arbaut

6/21/2005 1:16:00 PM

0




On 21/06/2005 15:12, Wayne Pierce wrote:

> I have a vendor product with Java APIs that I need to write against
> and would like to use Ruby.
>
> Does anyone have an opinion on the most reliable way to interact with
> Java from Ruby? I've seen some different projects, but can't tell if
> there is any major difference between how they are implemented or
> which are actively maintained.
>

Seems ugly to me, but anyway... Maybe you will be able to use C or C++ as a
common "glue code". There is also a program called SWIG, used to make Java,
Ruby, Python, etc... work with C.

Robert Klemme

6/21/2005 2:41:00 PM

0

Jean-Claude Arbaut wrote:
> On 21/06/2005 15:12, Wayne Pierce wrote:
>
>> I have a vendor product with Java APIs that I need to write against
>> and would like to use Ruby.
>>
>> Does anyone have an opinion on the most reliable way to interact with
>> Java from Ruby? I've seen some different projects, but can't tell if
>> there is any major difference between how they are implemented or
>> which are actively maintained.
>>
>
> Seems ugly to me, but anyway...

+1

I'd use plain old Java. That'll be much more hassle free and Java isn't
too bad IMHO.

> Maybe you will be able to use C or
> C++ as a common "glue code". There is also a program called SWIG,
> used to make Java, Ruby, Python, etc... work with C.

A third language in between - even uglier. But probably the only workable
way.

Kind regards

robert

threeve.org

6/21/2005 3:09:00 PM

0

On 6/21/05, Wayne Pierce <shalofin@gmail.com> wrote:
> I have a vendor product with Java APIs that I need to write against
> and would like to use Ruby.
>
> Does anyone have an opinion on the most reliable way to interact with
> Java from Ruby? I've seen some different projects, but can't tell if
> there is any major difference between how they are implemented or
> which are actively maintained.
>
> Thanks,
>
> Wayne
>
>

Two main options: RJB [1], and JRuby [2].

RJB (RubyJava Bridge) is an extension to the standard Ruby interpreter
that lets you communicate with a Java VM via JNI. It works pretty
well, but can be a bit 'low level' sometimes, due to the nature of its
implementation. IE, sometimes you need to know how to construct java
method signatures and such (not a big deal)

JRuby is a complete Ruby interpreter written in Java, so all your Ruby
code runs in the JVM. This has advantages that the classes can have a
tighter relationship, however the implementation is currently a little
slow, and partially incomplete. Also, you lose the ability to use
Ruby libraries and extensions written in C.

I've used RJB for a project in order to use the JasperReports library,
and had pretty decent success. It can be difficult to do proper error
handling, because you have both Ruby errors and Java exceptions to
deal with. But it is certainly feasible.

It works best when you simply want to call some java code from ruby,
and get the result. When you start wanting to have ruby classes
inherit from java classes, and have java callback to ruby which calls
back to java, it can get messy. Just try to keep it as simple as
possible.

Jason

[1] http://arton.no-ip.info/collabo/backyard/?Ruby...
[2] http://jruby.sourc...


Ara.T.Howard

6/21/2005 3:14:00 PM

0

Mark Probert

6/21/2005 3:23:00 PM

0

Hi ..

On Tuesday 21 June 2005 07:45, Robert Klemme wrote:
> Jean-Claude Arbaut wrote:
> > On 21/06/2005 15:12, Wayne Pierce wrote:
> >> I have a vendor product with Java APIs that I need to write against
> >> and would like to use Ruby.
> >>
>
> I'd use plain old Java. That'll be much more hassle free and Java isn't
> too bad IMHO.
>

Some of the other options than using POJ are:

Scala -- http://scal...
Nice -- http://nice.source...

Both of these are built on the JVM and use the JRE. With all respect to Ruby,
I have used both when in a Java environment rather than force Ruby into a
place where it is best not to go.

Regards,

--
-mark. (probertm at acm dot org)


Adam P. Jenkins

6/21/2005 4:16:00 PM

0

Wayne Pierce wrote:
> I have a vendor product with Java APIs that I need to write against
> and would like to use Ruby.
>
> Does anyone have an opinion on the most reliable way to interact with
> Java from Ruby? I've seen some different projects, but can't tell if
> there is any major difference between how they are implemented or
> which are actively maintained.

Here are some ways to interact with Java from Ruby, though I can't say
which is the most stable:

* JRuby: http://jruby.source...

* RJB: http://arton.no-ip.info/collabo/backyard/?Ruby...

* RJNI: http://thekode.net/ruby/rjni/...

Personally, I'd just use Java, or a more Java-compatible scripting
language like Beanshell (http://bea...) or Groovy
(http://groovy.cod...). I like Ruby, but I don't see the point
of jumping through hoops to use it for everything unless you need to
integrate some existing Ruby and Java code.

Wayne Pierce

6/21/2005 4:52:00 PM

0

> Seems ugly to me, but anyway...

It could get very ugly, but it may be saner in the long run for this
organization.

I had considered using POJ, but want to limit the number of languages
in use here. So far I have programs in Ruby, Python, shell (/bin/sh)
and sed scripts. I have also inherited some Perl, Tcl and may soon
receive C# and PHP code (I was able to turn away the VB).

I understand enough about each of the languages to work with the
programs, which is why they come to me when the other person leaves.
However if I ever leave there is nobody here that can pick apart all
of the different languages; I'd rather not add another one to the mix
(Java) if it can be prevented.

For this program I only need to call a function passing in two
parameters from the API. From what I read concerning some of the
suggestions it should be doable and shouldn't be too difficult.

Thanks for all the responses,

Wayne


Robert Klemme

6/21/2005 5:37:00 PM

0


"Mark Probert" <probertm@acm.org> schrieb im Newsbeitrag
news:200506210823.30148.probertm@acm.org...
> Hi ..
>
> On Tuesday 21 June 2005 07:45, Robert Klemme wrote:
>> Jean-Claude Arbaut wrote:
>> > On 21/06/2005 15:12, Wayne Pierce wrote:
>> >> I have a vendor product with Java APIs that I need to write against
>> >> and would like to use Ruby.
>> >>
>>
>> I'd use plain old Java. That'll be much more hassle free and Java isn't
>> too bad IMHO.
>>
>
> Some of the other options than using POJ are:
>
> Scala -- http://scal...
> Nice -- http://nice.source...
>
> Both of these are built on the JVM and use the JRE. With all respect to
> Ruby,
> I have used both when in a Java environment rather than force Ruby into a
> place where it is best not to go.

That's a good suggestion! Then we should mention groovy also which has some
similarities with Ruby. However, the last time I looked at it it was still
immature and also terribly slow...

http://groovy.cod...

Kind regards

robert

SAKURAI Masashi

6/21/2005 10:39:00 PM

0

Hi,

> I have a vendor product with Java APIs that I need to write against
> and would like to use Ruby.
>
> Does anyone have an opinion on the most reliable way to interact with
> Java from Ruby? I've seen some different projects, but can't tell if
> there is any major difference between how they are implemented or
> which are actively maintained.

I have another implementation Java-Ruby bridge that is written in pure
Java and Ruby.

YAJB
http://www.cmt.phys.kyushu-u.ac.jp/~M.Sakurai/cgi-bin/fw/wiki.cgi...

This bridge is implemented by Javassist and xmlrpc communication.
Javassist is bytecode engineering tool. I used xmlrpc to connect java
and ruby. Although the communication is very slow, the bridge can
establish connection on the many environment.

The YAJB is designed for achievement of following goals:

* easy to use (at least for me, on cygwin/windows XP)
* allowing simple and complete GUI code as we write in Java
* writing implementation of Java interfaces and any class in Ruby.

I wrote the code for my own purpose, analyzing numerical data and
visualizing it with my plot library, not general use.
So I'm sorry if you meet troubles. (I'm not sure using gem or setup.rb...)


Here is the sample YAJB code, handling modal JDialog:

=============================

#!/usr/bin/ruby

require 'yajb/jbridge'
include JavaBridge

jimport "java.awt.*"
jimport "java.awt.event.*"
jimport "javax.swing.*"

label = jnew :JLabel, "Hello World!"

button = jnew :JButton, "open!"
action = jextend :ActionListener
button.addActionListener(action)

panel = jnew :JPanel, jnew(:GridLayout,2,1)
panel.add(label)
panel.add(button)

frame = jnew :JFrame, "JDialog DEMO"
frame.getContentPane.add(panel)
frame.pack

class << action
def init(f,l)
@frame = f
@label = l
end

def actionPerformed(event)
dlg = jnew :JDialog, @frame, "Modal Dialog!", true

text = jnew :JTextField,12

okbtn = jnew :JButton,"OK"
action = jextend :ActionListener
class << action
def init(d,t,l)
@dlg = d; @text = t; @label = l
end
def actionPerformed(e)
@label.setText(@text.getText())
@dlg.dispose()
end
end
action.init(dlg,text,@label)
okbtn.addActionListener(action)

panel = jnew :JPanel
panel.add(text)
panel.add(okbtn)

dlg.getContentPane.add(panel)
dlg.pack
dlg.show
end
end

action.init(frame,label)

wc = jextend :WindowAdapter
class << wc
def windowClosing(e)
break_bridge
wakeup_thread
end
end
frame.addWindowListener(wc)
frame.show()

stop_thread

=======================