# Syntax to include custom Java classes

**URL:** <https://www.ruby-forum.com/t/syntax-to-include-custom-java-classes/150145>\
**Category:** Ruby\
**Created:** [October 4, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/syntax-to-include-custom-java-classes/150145 "2008-10-04T18:05:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiagobrandam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@thiagobrandam](https://www.ruby-forum.com/u/thiagobrandam)\
**Post date:** [October 4, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/syntax-to-include-custom-java-classes/150145/1 "2008-10-04T18:05:38Z")

</div>

Hi guys. This is a newbie’s question: how do I do to include custom  
Java classes in a .rb file? Do I have to make a .jar file and then  
call a require? If so, what’s the next thing I have to do to use a  
Java class in the same .rb file? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![thiagobrandam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@thiagobrandam](https://www.ruby-forum.com/u/thiagobrandam)\
**Post date:** [October 4, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/syntax-to-include-custom-java-classes/150145/2 "2008-10-04T21:30:46Z")

</div>

I figured out how to do.

---

<div class="post-metadata">

**Author:** ![thiagobrandam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@thiagobrandam](https://www.ruby-forum.com/u/thiagobrandam)\
**Post date:** [October 5, 2008, 2:17am UTC](https://www.ruby-forum.com/t/syntax-to-include-custom-java-classes/150145/3 "2008-10-05T02:17:56Z")

</div>

thiagobrandam wrote:

> I figured out how to do.

Side note: There’s a good change that if one person has a question  
about something, then someone else will, too.

If you found an answer for your problem it would be helpful to post it  
here so that others can find it or reference it.

(There’s another matter of whether the original question would have been  
better asked on the JRuby list, but here it is, so …)

–  
James B.

[www.happycamperstudios.com](http://www.happycamperstudios.com) - Wicked Cool Coding  
[www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff

---

<div class="post-metadata">

**Author:** ![thiagobrandam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@thiagobrandam](https://www.ruby-forum.com/u/thiagobrandam)\
**Post date:** [October 5, 2008, 4:15am UTC](https://www.ruby-forum.com/t/syntax-to-include-custom-java-classes/150145/4 "2008-10-05T04:15:00Z")

</div>

I don’t know if this is the best way but here it goes:

Every .class file that has a correspondent class I may use in a .rb  
file, I can merge them in a .jar file. Then, I just have to call  
require ‘name\_of\_the\_file.jar’ (if it’s in the same directory). To use  
a particular class, say ClassName, just call import ‘ClassName’.  
Everything else is pretty much the same as though you were only using  
Ruby (except for the fact that you must use jruby to interpret).

As for whether it would be better in a jruby list, I didn’t know that  
the scope of this one was MRI solely. Next time I’ll think twice  
before posting newbie questions in the wrong places. Sorry for any  
incovenience.

Thiago

---

<div class="post-metadata">

**Author:** ![thiagobrandam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@thiagobrandam](https://www.ruby-forum.com/u/thiagobrandam)\
**Post date:** [October 5, 2008, 5:52am UTC](https://www.ruby-forum.com/t/syntax-to-include-custom-java-classes/150145/5 "2008-10-05T05:52:39Z")

</div>

thiagobrandam wrote:

> I don’t know if this is the best way but here it goes:
> 
> Every .class file that has a correspondent class I may use in a .rb  
> file, I can merge them in a .jar file. Then, I just have to call  
> require ‘name\_of\_the\_file.jar’ (if it’s in the same directory). To use  
> a particular class, say ClassName, just call import ‘ClassName’.  
> Everything else is pretty much the same as though you were only using  
> Ruby (except for the fact that you must use jruby to interpret).

Cool! Thanks.

> As for whether it would be better in a jruby list, I didn’t know that  
> the scope of this one was MRI solely. Next time I’ll think twice  
> before posting newbie questions in the wrong places. Sorry for any  
> incovenience.

No inconvenience, and I also don’t know if JRuby questions are welcome  
here or not. The list is ruby-talk, not c-ruby-talk or mri-talk, so I  
vote Yes on general JRuby usage questions.

(Of course, I do a lot of work with JRuby, so I’m biased. But Ruby is  
Ruby, and this list seems amenable to question on using C extensions and  
such, so I think we’re all good.)

–  
James B.

[www.happycamperstudios.com](http://www.happycamperstudios.com) - Wicked Cool Coding  
[www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff

---

<div class="post-metadata">

**Author:** ![thiagobrandam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@thiagobrandam](https://www.ruby-forum.com/u/thiagobrandam)\
**Post date:** [October 5, 2008, 10:00am UTC](https://www.ruby-forum.com/t/syntax-to-include-custom-java-classes/150145/6 "2008-10-05T10:00:00Z")

</div>

On Sun, Oct 5, 2008 at 5:48 AM, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Cool! Thanks.  
> I agree this was useful info, I played around with this and had some  
> difficulties.
> 
> > As for whether it would be better in a jruby list, I didn’t know that  
> > the scope of this one was MRI solely. Next time I’ll think twice  
> > before posting newbie questions in the wrong places. Sorry for any  
> > incovenience.
> 
> No inconvenience, and I also don’t know if JRuby questions are welcome here  
> or not. The list is ruby-talk, not c-ruby-talk or mri-talk, so I vote Yes  
> on general JRuby usage questions.  
> Well the JRuby list might have be the place to look for an answer but  
> I agree that this list is general enough to share it or  
> even ask for it, so yes I share James’ POV here.

Thanks again and just another point. Unless you have a very good  
reason to top post please try bottom or inline posting the next time.  
This is a long established practice on this list. No big deal however.

## Cheers Robert

C’est véritablement utile puisque c’est joli.

Antoine de Saint Exupéry

---

<div class="post-metadata">

**Author:** ![thiagobrandam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@thiagobrandam](https://www.ruby-forum.com/u/thiagobrandam)\
**Post date:** [October 5, 2008, 3:04pm UTC](https://www.ruby-forum.com/t/syntax-to-include-custom-java-classes/150145/7 "2008-10-05T15:04:03Z")

</div>

> Thanks again and just another point. Unless you have a very good  
> reason to top post please try bottom or inline posting the next time.  
> This is a long established practice on this list. No big deal however.

Sorry guys, I’m not used to it. I’ll obey the rules of the list.  
Thanks for the help.
