Forum: JRuby Calling Java from JRuby problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C1623a8d4d3b7463a1963b5bd4cf0601?d=identicon&s=25 Fla As (fla)
on 2008-10-25 09:54
Attachment: _bashrc (93 Bytes)
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.

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

To create the jar archive I use "jar cf Test02.jar Test02.class".

[code]
require 'java'
require 'Test02.jar'

test = Test02.new()
test.output
[/code]

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
E2c04ac757951e740b7266be17d27064?d=identicon&s=25 James Way (jamescway)
on 2009-11-04 02:23
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.
>
> [code]
> public class Test02 {
>   public void output() {
>     System.out.print("From Java");
>   }
> }
> [/code]
>
> To create the jar archive I use "jar cf Test02.jar Test02.class".
>
> [code]
> require 'java'
> require 'Test02.jar'
>
> test = Test02.new()
> test.output
> [/code]
>
> 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
9d5a28f7f32c760abcf49ad155683181?d=identicon&s=25 Justin Coyne (Guest)
on 2009-11-04 22:30
(Received via mailing list)
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 Way <lists@ruby-forum.com> 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
This topic is locked and can not be replied to.