Re: Calling Java from JRuby problem

View: New views
2 Messages — Rating Filter:   Alert me  

Parent Message unknown Re: Calling Java from JRuby problem

by Peter Ritchie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

--
Posted via http://www.ruby-forum.com/.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: Calling Java from JRuby problem

by Justin Coyne :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...> wrote:

> 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
>
> --
> Posted via http://www.ruby-forum.com/.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email