Forum: JRuby Call Java static method

6aa2708826a954ac390fc47cddd2aae5?d=identicon&s=25 Lysa GS (gs84)
on 2012-03-30 01:51
Hi everybody,

I am trying to import some java class in order to use its methods.
The java class code is like the following (capital letters are written
like this):

public class SetNMOAndVKHF{

   //First method (name begins with Capital letter)
   public static void AddVKHF(Object param1, boolean params2)
                       throws IOException, Exception{
            //The method instructions
            ....
   }

   //second method
   private static void setTest(Type1 var1, Type2 var2) {
    //Instructions
       ....
  }
}

I want to import that class in my class called "MyClass" in JRuby
My Jruby code is the following:

include Java
java_import  java.lang.System
java_import  java.lang.Object

$CLASSPATH << "#{RAILS_ROOT}/lib/myfolder"
java_import com.folder1.folder2.SetNMOAndVKHF

Dir["#{RAILS_ROOT}/lib/jars/compatibles/*.jar"].each {|jar| require jar}

//MyClass is in The Rails app/models directory
class MyClass
   def initialize()
   end

   def testme
      ....
      SetNMOAndVKHF.AddVKHF(param1, param2)
      ....
   end
end


Then i am calling "MyClass" in some controller like this
test = MyClass.new
test.testme

But while executing i am getting the following error:
 "SetNMOAndVKHF(ArgumentError) A copy of MyClass has been removed from
the module tree but is still active!

 Before that got this error "Java package com.folder1.folder2 does not
have a method const_get() "

In debug mode, those errors happen at the beginning, on the first line
of MyClass (before the line calling my java class method).


So my question is how can i import the Java class "SetNMOAndVKHF", then
use its "AddVKHF" method (with particularity method name begins with
Capital letter).

Can someone explains me how to avoid these errors.

Thanks in advance for your help.
F1d37642fdaa1662ff46e4c65731e9ab?d=identicon&s=25 Charles Nutter (headius)
on 2012-03-30 06:42
(Received via mailing list)
On Thu, Mar 29, 2012 at 11:51 PM, Salimata Gueye <lists@ruby-forum.com>
wrote:
> I am trying to import some java class in order to use its methods.
> The java class code is like the following (capital letters are written
> like this):

...class with static capitalized method name...

> Dir["#{RAILS_ROOT}/lib/jars/compatibles/*.jar"].each {|jar| require jar}
...
>      SetNMOAndVKHF.AddVKHF(param1, param2)
...
> Then i am calling "MyClass" in some controller like this
> test = MyClass.new
> test.testme

This looks ok. I also tested a class locally with a capitalized static
method and it worked fine.

> But while executing i am getting the following error:
>  "SetNMOAndVKHF(ArgumentError) A copy of MyClass has been removed from
> the module tree but is still active!

That method usually indicates that Rails has detected a source change
and reloaded your files, but then discovered that one of the constants
it reloaded still has an object in use.

>  Before that got this error "Java package com.folder1.folder2 does not
> have a method const_get() "

This looks like Rails trying to do some constant magic against our
"special" Java package modules.

> In debug mode, those errors happen at the beginning, on the first line
> of MyClass (before the line calling my java class method).
>
>
> So my question is how can i import the Java class "SetNMOAndVKHF", then
> use its "AddVKHF" method (with particularity method name begins with
> Capital letter).

Could you provide the full backtraces for the errors? Something's not
right here...

- Charlie
6aa2708826a954ac390fc47cddd2aae5?d=identicon&s=25 Lysa GS (gs84)
on 2012-03-30 10:42
Attachment: full_error_backtrace.jpg (700 KB)
Thank you Charlie for your answer.

When running in debugg mode i am getting this in my trace:

SetNMOAndVKHF::AddVKHF = {ArgumentError} A copy of
Java::JavaLang::Object has been removed from the module tree but is
still active!

Does Ruby interpretor consider my "AddVKH" method as a class instead of
a class method???

The full error's backtrace is in the attached file.

Thanks in advance for your help!
6aa2708826a954ac390fc47cddd2aae5?d=identicon&s=25 Lysa GS (gs84)
on 2012-03-30 11:30
Attachment: const_get_full_error_backtrace.jpg (600 KB)
But when remove the following line:
java_import  java.lang.Object

I am getting the following error
SetNMOAndVKHF::AddVKHF = {ArgumentError} Java package
`com.folder1.folder2' does not have a method `const_get'

Please see full error backtrace in attached file.
6aa2708826a954ac390fc47cddd2aae5?d=identicon&s=25 Lysa GS (gs84)
on 2012-04-02 11:05
Charles Nutter wrote in post #1054131:

>
>>  Before that got this error "Java package com.folder1.folder2 does not
>> have a method const_get() "

>
> Could you provide the full backtraces for the errors? Something's not
> right here...
>

I have provided the full backtraces for this error... can someone help
me solve this error because i still getting the same error untill now.

Thanks in advance for your help.
F1d37642fdaa1662ff46e4c65731e9ab?d=identicon&s=25 Charles Nutter (headius)
on 2012-04-02 15:27
(Received via mailing list)
On Fri, Mar 30, 2012 at 4:30 AM, Lysa GS <lists@ruby-forum.com> wrote:
> But when remove the following line:
> java_import  java.lang.Object

You should *definitely* not do this. Object is a core Ruby class, and
importing Java's Object will overwrite it and cause all sorts of
terrible things to happen. I'm sorry I did not notice this before.

> I am getting the following error
> SetNMOAndVKHF::AddVKHF = {ArgumentError} Java package
> `com.folder1.folder2' does not have a method `const_get'

You are getting this when calling the method?

I'm afraid I don't have enough information yet to understand the
issues you're having. Do remove the import of java.lang.Object and
provide the complete script plus a link to the API you're trying to
call.

- Charlie
6aa2708826a954ac390fc47cddd2aae5?d=identicon&s=25 Lysa GS (gs84)
on 2012-04-26 11:57
Charles Nutter wrote in post #1054604:

On 2 avr, 15:22, Charles Oliver Nutter <head...@headius.com> wrote:
> On Fri, Mar 30, 2012 at 4:30 AM, Lysa GS <li...@ruby-forum.com> wrote:

> I'm afraid I don't have enough information yet to understand the
> issues you're having. Do remove the import of java.lang.Object and
> provide the complete script plus a link to the API you're trying to
> call.
>
> - Charlie


Thank you Charlie for your help. I ve changed the version of the CXF
library i was using and this solve my problem.

Now i am facing another problem when trying to extend a java abstract
class with 2 methods with same name. I ve tried two methods but i still
can't make them work:
1) For the first one i tried to completly rewrite the class in jruby at
the following link:   http://www.ruby-forum.com/topic/4095917#new

2) For the 2nd one i tried to extend the abstract class in Jruby at the
following link :
http://stackoverflow.com/questions/10295739/jruby-...

Any help will be appreciate to help me solve and understand these
solutions.

Thanks in advance for your help.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.