Accessing java classes in Jruby on Rails

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

Hi,

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

Thanks,
–Vladimir

On Tue, Mar 9, 2010 at 12:44 PM, Veena J. [email protected]
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

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

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 C. [email protected]
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

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

class FooController < ApplicationController

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

end

On Thu, Mar 18, 2010 at 7:19 AM, Veena J. [email protected]
wrote:


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

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 J. [email protected]
wrote:

def index


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

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 J. removed_email_addres[email protected] 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

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:inmethod_missing’
from (irb):7:in `irb_binding’

Thanks,
Max

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 [email protected]
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

On Fri, Mar 19, 2010 at 4:10 PM, Max De marzi [email protected]
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! :slight_smile: If you do that, it should work fine.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

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

Mother pus bucket!

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs