# Can't execute method of Java object from Ruby

**URL:** <https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505>\
**Category:** JRuby\
**Created:** [November 20, 2015, 1:30pm UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505 "2015-11-20T13:30:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [November 20, 2015, 1:30pm UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/1 "2015-11-20T13:30:33Z")

</div>

jruby 9.0.4.0 (2.2.2) 2015-11-12 b9fb7aa Java HotSpot™ 64-Bit Server  
VM 24.79-b02 on 1.7.0\_79-b15 +jit [Windows 7-amd64]

I have the following Java class:

# ================================================== class JC { private Integer vi; public JC(int i) { vi = new Integer(i); } public void square() { vi = vi\*vi; } public Integer get\_int() { return vi; } }

I create (in the Java part of my code) an object of this class, and pass  
it to a Ruby method. On the Ruby side, I find that I can’t invoke the  
method ‘square’, even though the object seems to be of the right type:

==================================================  
include Java

class RCconn

def initialize  
@doer = RC.new  
end

def dispatch(jc)  
puts “Received object of type #{jc.class}”  
jc.square  
# @doer.do\_your\_duty(jc)  
jc  
end

# end

Inside dispatch, I get:

Received object of type Java::Default::JC  
NoMethodError: undefined method `square’ for  
#Java::Default::JC:0xa124e5  
dispatch at …

What did I do wrong?

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [November 20, 2015, 3:49pm UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/2 "2015-11-20T15:49:40Z")

</div>

Ronald,

Put the public modifier before your java class:

public class JC {  
private Integer vi;  
public JC(int i) { vi = new Integer(i); }  
public void square() { vi = vi\*vi; }  
public Integer get\_int() { return vi; }  
}

Move it to its own file and try again. The method, as you have it, is  
not accessible to Ruby.

Cris

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [November 20, 2015, 4:20pm UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/3 "2015-11-20T16:20:34Z")

</div>

Cris S. wrote in post #1179417:

> Ronald,
> 
> Put the public modifier before your java class

Waaaaa! This works!!!

Could you explain, please, why this is necessary? I had JC defined in  
its own file before, and I could executed all its methods (they are  
public, after all) from my Java application. Why, then, can’t Ruby see  
it? Does this have to do with package visibility? But I didn’t use any  
package declaration.

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [November 20, 2015, 6:21pm UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/4 "2015-11-20T18:21:29Z")

</div>

Since you are all setup with this example…

Answer for me a question.

JRuby has the java\_package method. If you set your Ruby class to the  
same package can you access your method w/o the public modifier?

Cris

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [November 20, 2015, 6:18pm UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/5 "2015-11-20T18:18:32Z")

</div>

The other java files were in the same package, so they can see the  
package level methods.

The private, protected, public (and java has no modifier – package  
access (or friendly)) are a bit different then Ruby. I came from the  
java side first and I often get confused on the Ruby side :-).

Cris

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [November 23, 2015, 11:40am UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/6 "2015-11-23T11:40:53Z")

</div>

Cris S. wrote in post #1179422:

> JRuby has the java\_package method. If you set your Ruby class to the  
> same package can you access your method w/o the public modifier?

It didn’t work, but I’m not sure, whether I did it in a right way. Since  
the Java class JC is in the global namespace, I added to my Ruby file  
after the include Java:

```
java_package ''

```

But maybe this is not the correct way to specify the global namespace. I  
searched several places for an explanation of how java\_package works,  
but they don’t explain handling the global namespace.

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [November 23, 2015, 5:16pm UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/7 "2015-11-23T17:16:23Z")

</div>

I’m still fighting with how to do packages properly in Java (I’ve opened  
a thread on this at  
[http://www.javaprogrammingforums.com/whats-wrong-my-code/46314-can-not-access-name-same-package-but-different-file-beginners-question.html](http://www.javaprogrammingforums.com/whats-wrong-my-code/46314-can-not-access-name-same-package-but-different-file-beginners-question.html))

- maybe, if you have time, you could have a look. It seems to be more  
difficult than expected, and as soon as I have a solution for this, I  
will try out java\_package.

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [November 23, 2015, 3:23pm UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/8 "2015-11-23T15:23:00Z")

</div>

I am not sure either how one would handle an unnamed package. I suspect  
you might have to package your code.

Cris

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [November 23, 2015, 5:53pm UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/9 "2015-11-23T17:53:41Z")

</div>

Ronald,

Are you aware that in java package structures mandate that your file be  
in an appropriate directory or compilation fails?

If you have package ‘phee.phi.pho’ Then class ‘Phum’ must be in the  
directory phee/phi/pho. It must be in a file named Phum.java if it is  
has the public modifier before the keyword class.

Cris

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [November 23, 2015, 6:17pm UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/10 "2015-11-23T18:17:42Z")

</div>

Cris S. wrote in post #1179475:

> Are you aware that in java package structures mandate that your file be  
> in an appropriate directory or compilation fails?

I thought this is just a convention. It is strange: The compiler could  
see that the package name does not match the filename, but it did not  
complain (“file in a wrongly named directory” or something like this)  
when I compiled it.

I will try to modify my example and let you know the findings.

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [November 24, 2015, 10:26am UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/11 "2015-11-24T10:26:43Z")

</div>

Cris S. wrote in post #1179475:

> If you have package ‘phee.phi.pho’ Then class ‘Phum’ must be in the  
> directory phee/phi/pho. It must be in a file named Phum.java if it is  
> has the public modifier before the keyword class.

I have now restructured file structure and code accordingly, but running  
the program now doesn’t find the main class.

I have attached all files to this message (jr7.zip), in case you would  
like to hava a look. On Windows, I would run it by executing jr7.bat.

I have also isolated the error, since it is not related to java\_import,  
and posted the problem here:

> **[Blab Tech](https://tech.blab.com/java-programming-forums)**
>
> Blab Tech covers the latest technology headlines, innovations, AI, hardware, and general tech news.

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [November 24, 2015, 12:41pm UTC](https://www.ruby-forum.com/t/cant-execute-method-of-java-object-from-ruby/243505/12 "2015-11-24T12:41:23Z")

</div>

OK, fixed it now. There were several small changes necessary, which were  
not obvious - at least not to me. You find the updated test case in the  
attachment.

As for the java\_package on the Ruby side: This did not work as expected.  
I get the error message

NameError: cannot load Java class DoIt

I then changed back

java\_import ‘DoIt’ do …

to

java\_import ‘foo.DoIt’ do …

(although I think this should not be necessary, because we have a  
java\_package ‘foo’ before that), but same effect.
