Calling Java from JRuby problem


#1

Hello,
I was playing to load my own Java code in JRuby, but my little example
does not work and I do not understand why.

public class Test02 {
  public void output() {
    System.out.print("From Java");
  }
}

To create the jar archive I use “jar cf Test02.jar Test02.class”.

require 'java'
require 'Test02.jar'

test = Test02.new()
test.output

When I start “jruby Test02.rb” I always get this error:
/home/mitlox/jruby-1.1.4/lib/ruby/site_ruby/1.8/builtin/javasupport.rb:1:in
`const_missing’: uninitialized constant Test02 (NameError)
from Test02.rb:6

Perhaps I must set classpath variable but I have no idea how. Maybe do
you any idea what I did wrong with my little example?

Best regards


#2

I’m not sure but maybe you need to put the full path to your jar file.
I am new at this also.

require ‘/Users/myname/jar/Test02.jar’

TestClasses = Java::Test02.Test02 #package.class, here assuming pkg
Test02

someVar = TestClasses.Test02.new
someVar.output

Fla As wrote:

Hello,
I was playing to load my own Java code in JRuby, but my little example
does not work and I do not understand why.

public class Test02 {
>   public void output() {
>     System.out.print("From Java");
>   }
> }

To create the jar archive I use “jar cf Test02.jar Test02.class”.

require 'java'
> require 'Test02.jar'
> 
> test = Test02.new()
> test.output

When I start “jruby Test02.rb” I always get this error:
/home/mitlox/jruby-1.1.4/lib/ruby/site_ruby/1.8/builtin/javasupport.rb:1:in
`const_missing’: uninitialized constant Test02 (NameError)
from Test02.rb:6

Perhaps I must set classpath variable but I have no idea how. Maybe do
you any idea what I did wrong with my little example?

Best regards


#3

If your class is in a package you need to specify it.

package org.myname.foo

public class Test02 { … }

this becomes:

Java::OrgMynameFoo::Test02.new

Otherwise you can call java_import()

java_import(‘org.myname.foo.Test02’)
Test02.new

-Justin

On Tue, Nov 3, 2009 at 7:23 PM, James W. removed_email_address@domain.invalid wrote:

public void output() {

you any idea what I did wrong with my little example?


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email