Forum: JRuby Accessing java classes in Jruby on Rails

Posted by Veena Jose (veena)
on 2010-03-09 12:44
Hai friends,

    I am a newbie in JRuby on Rails.I have so many java classes which i
need to integrate with my Jruby on Rails project.
Can you please tell me what are the steps for doing the same.....

Thanks,
Veena
Posted by Vladimir Sizikov (Guest)
on 2010-03-09 12:48
(Received via mailing list)
Hi,

Take a look here: 
http://kenai.com/projects/jruby/pages/CallingJavaFromJRuby

Thanks,
  --Vladimir

On Tue, Mar 9, 2010 at 12:44 PM, Veena Jose <lists@ruby-forum.com> 
wrote:
> 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
Posted by Veena Jose (veena)
on 2010-03-18 13:19
Hai Vladimir,

Thanks for the link.....It was certainly informative......
But can you give me one example where a simple java class function is 
being accessed by a controller of Jruby on rails and display the result 
through its view...

From internet i am not getting a clear cut idea as what to do.....

If anyone can help please help me.....

Thanks,
Veena
Posted by Justin Coyne (justincoyne)
on 2010-03-18 14:46
(Received via mailing list)
class FooController < ApplicationController

  def index
    render :text=>org.foo.MyClass.new.myFunction()
  end

end

On Thu, Mar 18, 2010 at 7:19 AM, Veena Jose <lists@ruby-forum.com> 
wrote:
>
>
>

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

    http://xircles.codehaus.org/manage_email
Posted by Charles Nutter (headius)
on 2010-03-18 18:18
(Received via mailing list)
Yeah, it's really this easy.

1. require 'java' in that file
2. java_import classes (or reference them directly like Justin has below
3. use the classes

On Thu, Mar 18, 2010 at 8:46 AM, Justin Coyne <digger250@gmail.com> 
wrote:
>>
>> Veena
>
> ---------------------------------------------------------------------
> 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
Posted by Veena Jose (veena)
on 2010-03-19 06:53
Thank you Justin and Charles......But even now i am facing problem....

I want a jar file which contains only a simple java file (say 
'Factorial.java')to be accessible in Jruby.

foo_controller.rb
  require 'java'
  require '\test\test.jar'
class FooController < ApplicationController

  def index
    render :text=>Factorial.new.fact(5)
  end
end

"test" folder which contains the jar file is in "lib" folder

when i run the program it is showing error
"uninitialized constant FooController::Factorial"

Am i doing in a right way?please do correct me.....

Thanks,
Veena
Posted by Vladimir Sizikov (Guest)
on 2010-03-19 08:00
(Received via mailing list)
Hi,

So, you're trying to use Factorial class from default package?
If yes, then you need to use: Java::Factorial

If no, then you need to use fully-qualified class name:
org.foo.Factorial or to java_import org.foo.Factorial
after require 'java'.

Thanks,
  --Vladimir

On Fri, Mar 19, 2010 at 6:53 AM, Veena Jose <lists@ruby-forum.com> 
wrote:
>  def index
>
>
>

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

    http://xircles.codehaus.org/manage_email
Posted by Veena Jose (veena)
on 2010-03-19 12:18
> If no, then you need to use fully-qualified class name:
> org.foo.Factorial or to java_import org.foo.Factorial

Hai Vladimir ,
 i am sorry.....what is this fully-qualified class name?
I am trying to include a java class(user defined) which is in a jar file 
into jruby on rails.....

1.I created Factorial.java file which contains fact() fn.
2.I jarred the file into test.jar
Now i want to use the fact() fn in my FooController/index.
Where i should put this jar file?

I am sorry if i am troubling you people a lot.I certainly need your 
help...

Thanks,
Veena
Posted by Charles Nutter (headius)
on 2010-03-19 14:01
(Received via mailing list)
Java source files must be compiled into a .class file and then you can
put that .class file into the jar. You may want to walk through a
basic Java tutorial.

- Charlie (mobile)

On Mar 19, 2010, at 6:18 AM, Veena Jose <lists@ruby-forum.com> wrote:

> 1.I created Factorial.java file which contains fact() fn.
> 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
Posted by Max De marzi (maxdemarzi)
on 2010-03-19 22:10
Assuming

1. Java code compiled into class file
2. Jar file made from class file
3. Jar file required (require "lib/java/xxx/yyy.jar")

What are some possible reasons for getting:

@test = org.xxx.yyy.Zzzz.new("a").helloWorld()

NameError: cannot load Java class org.xxx.yyy.Zzzz
        from 
/home/max/.rvm/rubies/jruby-1.4.0/lib/ruby/1.8/irb/ruby-token.rb:102:in 
`get_proxy_or_package_under_package'
        from 
/home/max/.rvm/rubies/jruby-1.4.0/lib/ruby/site_ruby/shared/builtin/javasupport/java.rb:51:in 
`method_missing'
        from (irb):7:in `irb_binding'

Thanks,
Max
Posted by straightflush@gmail.com (Guest)
on 2010-03-19 22:23
(Received via mailing list)
I do this a couple of ways

module MyClass
  include_package "factory"
end

then in your controller

 def index
   MyClass::Factory.new
 end

 -- or --

you can alias it in your controller like

 def Factory
    Java::Factory
 end

 Then you can use Factory.new as if it were a native class

AD


On Fri, Mar 19, 2010 at 5:10 PM, Max De marzi <lists@ruby-forum.com> 
wrote:
> NameError: cannot load Java class org.xxx.yyy.Zzzz
> --
> 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
Posted by Charles Nutter (headius)
on 2010-03-19 23:55
(Received via mailing list)
On Fri, Mar 19, 2010 at 4:10 PM, Max De marzi <lists@ruby-forum.com> 
wrote:
> NameError: cannot load Java class org.xxx.yyy.Zzzz
Java does require that the directory structure in the jar file match
the package structure. So within the jar, you'd need
org/xxx/yyy/Zzzz.class to be an entry.

Them's the rules! :) If you do that, it should work fine.

- Charlie

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

    http://xircles.codehaus.org/manage_email
Posted by Max De marzi (maxdemarzi)
on 2010-03-20 02:51
Mother pus bucket!

I've been bashing my head against the monitor all because I had a /src 
in my directory structure...

Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.