Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[Dltk-dev] Calling Java from Ruby

How can I call static Java methods located in a different project than my Ruby script?

I have a Java project named ZooGlue containing the class zoo.util.Truthiness, and a Ruby project named Zuby containing the script TestTruthiness.rb starting with these lines:

   include Java
   include_class Java::zoo.utils.Truthiness

When I run TestTruthiness.rb, I get
C:/downloads/Ruby/jruby-1.1.4/lib/ruby/site_ruby/1.8/builtin/javasupport.rb:49: in `method_missing': cannot load Java class zoo.utils.Truthiness (NameError)
   from C:\Workspace\Zuby\TestTruthiness.rb:9

I probably ought to put ZooGlue on Zuby's build path somehow, but the Build Path dialog box for the Ruby project does not list any of my Java projects as eligible to add to the build path. Should I explicitly refer to C:\Workspace\ZooGlue\bin\zoo\utils\Truthiness.class in the Ruby script somehow?

I am using Eclipse 3.4, DLTK 9.5, and my Ruby Interpreter executable is C:\downloads\Ruby\jruby-1.1.4\bin\jruby.bat

######################################################################
# Project: Zuby
# File: TestTruthiness.rb

include Java
include_class Java::zoo.utils.Truthiness

def show(it)
 println "" + it + ":\t" + isTrue(it) + "\t" + isFalse(it);
 println "\t" + (it ? "true":"false") + "\t" + ((!it)?"true":"false")
end

0 ? "TRUE" : "FALSE"

show( true )
show( false )
show( 0 )
show( 1 )
show( null )
show( "bye" )
show( "" )
show( 'x' )

######################################################################

//////////////////////////////////////////////////////////////////////
// Project: ZooGlue
// File: Truthiness.java

package zoo.utils;

/**
* Everything is true except false and null.
*/
public class Truthiness {
static public boolean isFalse (Object o)
   {
       return o == null || Boolean.FALSE.equals(o);
   }

   static public boolean isTrue (Object o)
   {
       return isFalse(o) == false;
   }
}

//////////////////////////////////////////////////////////////////////




Back to the top