Talk:Calling Java from JRuby
From JRubyWiki
How about if someone creates a Calling Ruby from Java? That content is listed under Java Integration
More info on calling third party classes?
Ok. I'm live now. The system let me create a comment before I confirmed my account email, but silently failed when I tried to post it.
So here's my comment: Will we see additional information on accessing 3rd party classes? I have a number of notes on the subject, but I'm having a hard time with the large number of test permutations. Here's what I have so far (all of it subject to change):
1. Put the JAR files in the CLASSPATH
(but this may be irrelevant. See step 2)
-or-
put them in $RUBY_HOME/lib (haven't tried this)
-or-
for NetBeans on windows, in JRUBY_EXTRA_CLASSPATH
AND 2. Require each jar by name, specifying the full path
!-The full path is required, even when in the CLASSPATH
So this works:
require "/some/path/MyStuff.jar"
But not this:
require "MyStuff.jar"
It fails with "no such file to load". That behavior must
be considered a bug. (The whole idea of the CLASSPATH is
to isolate the code from path changes)
! With the full path specified in the require statement,
it seems to work, _whether or not_ the CLASSPATH is set.
--the CLASSPATH does not seem to be accessed when set via
the CSH setenv function
--that would explain why the full path is needed
AND 3. Do an include_class on each class I want to access,
use a fully qualified package name.
-or-
Using the Java:: prefix the fully qualified package
name when naming one of classes in the code.
So if this method returns a Foo object:
x = Java::some.package.MyClass.staticGetMethod()
then:
include_class is /not/ necessary for MyClass
include_class /is/ necessary for Foo
Note:
Doing an optional include_class on some.package.MyClass does /not/ let you use a shorthand reference like Java::MyClass in the code. You still need the fully qualified package name (Java::some.package.MyClass)
$CLASSPATH
I have found that adding a jar to the $CLASSPATH from within a jruby script will give me access to the classes in it.
$CLASSPATH.append '/path/to/jar.jar'
MC = Java::ComMypackage::MyClassFromJarFile
But $CLASSPATH is a weird variable. The append method isn't on other arrays, and many array methods are not on $CLASSPATH. It must be an anonymous class for all I can tell.
-- Yes, $CLASSPATH is a special variable that modifies the classloading logic. It's not quite a normal array, so we can be notified when elements are appended to it and update accordingly. Headius
JRuby Applet ?
Could you provide an example Hello World applet in JRuby? thank you.

