# How to call a java function in rubyonrails method

**URL:** <https://www.ruby-forum.com/t/how-to-call-a-java-function-in-rubyonrails-method/57824>\
**Category:** Rails\
**Created:** [April 14, 2006, 3:55pm UTC](https://www.ruby-forum.com/t/how-to-call-a-java-function-in-rubyonrails-method/57824 "2006-04-14T15:55:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![matteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f9ae1b/32.png) [@matteo](https://www.ruby-forum.com/u/matteo)\
**Post date:** [April 14, 2006, 3:55pm UTC](https://www.ruby-forum.com/t/how-to-call-a-java-function-in-rubyonrails-method/57824/1 "2006-04-14T15:55:19Z")

</div>

Hi everyone,  
I’m Italian and sorry for my english mistakes…

I’m developing a web-site using rubyonrails.  
It’s REALLY COOL!  
But I’m obliged to re-use some javascript functions.  
Do you know where I can get the right documentation (and examples) in  
order to integrate these two different environments?

Thanks,  
Matteo.

---

<div class="post-metadata">

**Author:** ![matteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f9ae1b/32.png) [@matteo](https://www.ruby-forum.com/u/matteo)\
**Post date:** [April 14, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/how-to-call-a-java-function-in-rubyonrails-method/57824/2 "2006-04-14T17:05:39Z")

</div>

It’s very simple, just install a JVM and invoke your class from ruby  
like  
this:

`java -cp #{YOUR_JARPATH} #{YOUR_JAVACLASS} #{YOUR_ARGUMENTS}`

---

<div class="post-metadata">

**Author:** ![matteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f9ae1b/32.png) [@matteo](https://www.ruby-forum.com/u/matteo)\
**Post date:** [April 14, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/how-to-call-a-java-function-in-rubyonrails-method/57824/3 "2006-04-14T17:24:01Z")

</div>

Am 14.04.2006 um 15:55 schrieb Matteo:

> Hi everyone,  
> I’m Italian and sorry for my english mistakes…
> 
> I’m developing a web-site using rubyonrails.  
> It’s REALLY COOL!  
> But I’m obliged to re-use some javascript functions.  
> Do you know where I can get the right documentation (and examples) in  
> order to integrate these two different environments?

Do you mean JavaScript (aka ECMAScript, JScript) or Java (as in Sun  
Java, J2EE and Applets).

The latter case has already been answered and the earlier case -  
well. What about this in your templates:

Call Me!.

\*m

---

<div class="post-metadata">

**Author:** ![matteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f9ae1b/32.png) [@matteo](https://www.ruby-forum.com/u/matteo)\
**Post date:** [April 15, 2006, 2:21am UTC](https://www.ruby-forum.com/t/how-to-call-a-java-function-in-rubyonrails-method/57824/4 "2006-04-15T02:21:34Z")

</div>

> > But I’m obliged to re-use some javascript functions.  
> > Do you know where I can get the right documentation (and examples) in  
> > order to integrate these two different environments?  
> > In particolar I have to call this function from the controller, not from  
> > the view. Is it possible? And what about the arguments? And what should  
> > I do to handle the return result?

Can you explain a little more about what these existing functions are,  
how  
they are supposed to work, how you invoked them in the past, etc?

---

<div class="post-metadata">

**Author:** ![matteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f9ae1b/32.png) [@matteo](https://www.ruby-forum.com/u/matteo)\
**Post date:** [April 14, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/how-to-call-a-java-function-in-rubyonrails-method/57824/5 "2006-04-14T17:57:48Z")

</div>

> Do you mean JavaScript (aka ECMAScript, JScript) or Java (as in Sun  
> Java, J2EE and Applets).
> 
> The latter case has already been answered and the earlier case -  
> well. What about this in your templates:
> 
> Call Me!.
> 
> \*m

Yes, it’s the last case!  
In particolar I have to call this function from the controller, not from  
the view. Is it possible? And what about the arguments? And what should  
I do to handle the return result?

Thanks you a lot!  
M#

---

<div class="post-metadata">

**Author:** ![matteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f9ae1b/32.png) [@matteo](https://www.ruby-forum.com/u/matteo)\
**Post date:** [April 15, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/how-to-call-a-java-function-in-rubyonrails-method/57824/6 "2006-04-15T16:48:40Z")

</div>

> Can you explain a little more about what these existing functions are,  
> how  
> they are supposed to work, how you invoked them in the past, etc?

It’s a little bit difficult to explain, but I’ll try with an example.  
Now my view is just made with a simple input box where you can put your  
address. This input box action calls the javafunction.  
My javascript function has 3 different parts for 3 different steps.

1. The first one has to open an xml file, where all the information  
about the people are stored.
2. The second one has to fill some variables from this file, for example  
the phone number of the user who is login.
3. And the last one has to use these variables in order to create a  
‘result\_div’.

Now I have to reach some other goals. In particolar the final goal is to  
have a query in my database in order to understand if the phone number  
is already stored. According to the result I have to show a different  
view. Then I have to invoke the second javascript function in order to  
complete the ‘result\_div’.  
So I thought to split the original java function in 2 parts and to make  
a wrapper of the original java function like this.

* * *

## (controller) def wrapper(address) phone\_number=javascript:YourNumber(filename,address); @mobile.find(:first, conditions=\>[“phone\_number=?)”,phone\_number]) javascript:FunctionCall(); end

(view)  
\<%start\_form\_tag :action =\> ‘wrapper’%\>  
\<%= text\_field\_tag ‘address’%\>

  
\<%end%\>  
\<% if @mobile%\>  
It’s already stored.  
\<%else%\>  
Congratulation. Now you’re online.  
\<%end%\>
 

So I hope you understood. I did my best… As you understood my case is  
more complicated and I REALLY DIDN’T want to bore you! But if you read  
everything THANK YOU SO MUCH!!!  
So I have the follow questions: in order to make a wrapper I need to  
know  
-how may I pass the argument ADDRESS to the method wrapper.  
-how may I call the javafunction from the method.

THANK YOU SO MUCH, and when the site will be online you will be the  
first to know!  
FEEL FREE TO TELL ME YOUR OPINION!  
Matteo

---

<div class="post-metadata">

**Author:** ![matteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f9ae1b/32.png) [@matteo](https://www.ruby-forum.com/u/matteo)\
**Post date:** [April 15, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/how-to-call-a-java-function-in-rubyonrails-method/57824/7 "2006-04-15T21:41:36Z")

</div>

Thank you so much!  
Now the situation is quite clear.  
Thanks,  
M#

---

<div class="post-metadata">

**Author:** ![matteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f9ae1b/32.png) [@matteo](https://www.ruby-forum.com/u/matteo)\
**Post date:** [April 15, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/how-to-call-a-java-function-in-rubyonrails-method/57824/8 "2006-04-15T20:15:02Z")

</div>

> Now I have to reach some other goals. In particolar the final goal is to  
> have a query in my database in order to understand if the phone number  
> is already stored. According to the result I have to show a different  
> view. Then I have to invoke the second javascript function in order to  
> complete the ‘result\_div’.

OK, I think I see basically what you’re trying to do. You may be able  
to use  
something like ruby-js  
[http://raa.ruby-lang.org/project/ruby-js/](http://raa.ruby-lang.org/project/ruby-js/)  
[http://www.rubyist.net/~tamura/ruby/ruby-js/](http://www.rubyist.net/~tamura/ruby/ruby-js/)  
to run your Javascript function server-side.

My first stab at a problem like this would probably punt on reuse and  
port;  
looking something like this perhaps:

View  
\<% form\_remote\_tag :url =\> {:action =\> ‘wrapper’},  
:update =\> mobile\_div %\>  
\<%= text\_field\_tag ‘address’%\>  
\<%= submit\_tag %\>  
\<% end\_form\_tag %\>

```
<div id="mobile_div"> </div>

<div id="result_div"> </div>

```

Controller  
def wrapper(address)  
phone\_number = new\_ruby\_xml\_parseing\_function(address);  
if @mobile.find(:first,  
conditions=\>[“phone\_number=?)”,phone\_number])  
render\_text “It’s already stored.”  
else  
render\_text “Congratulation. Now you’re online.”  
end  
end

For the result\_div, depending on what it needs to do my first thoughts  
would be to either use the forms :complete callback to run the existing  
javascript function or build the result\_div server side in ruby and use  
an  
rjs template to fill it in.
