Re: [jruby-user] How to use custom class files in Rails?

snacktime <[email protected]> Sun, 9 Nov 2014 13:00:17 -0800
Newsgroups gmane.comp.lang.jruby.user
Message-ID <CABFnara4Eoy3cqh72nJJF6r987sJnv6JTboSMBc=Odq1XGMFjg@mail.gmail.com>
First off you don't want to just compile stuff by hand and use class
files.  Use a tool like gradle or maven to compile your java source into
proper jars.  I prefer gradle personally.  That said you *can* just dump
your .class file into lib and it will work fine.

lib is the directory that jruby sets up as the classpath.  It's the only
directory you can put jar or class files into and have things work
correctly without using -J-cp to set the classpath yourself on the command
line.  Yes you can put jars in other locations and just do a require on
them, but that can break any dynamic loading that's done in java
(Class.forName, etc..).  So when you find java throwing errors about not
finding jdbc drivers and such, that's why.

Personally I don't like just dumping jars in lib, that's my ruby directory.
  So I set the classpath explicitly and place my jars elsewhere.  The
simplest way to do this is to specify a directory ending with /*.  Java
will expand that so your classpath is then an explicit list of all files in
the directory.  Shaded or all in one jars make it more acceptable to just
keep using lib as you then just have a single jar.  This is more of a
personal preference thing imo.

After a LOT of trial and error, I found that the best way to run a jruby
program is to have a wrapper.   It's pretty rare you are going to just use
the default JVM settings for memory and such, and then when you throw the
classpath settings in you end up with a rather large command line that is a
pain to have to type in all the time.  Following is an example of a wrapper
for a ruby script (such as a gem binary)  that sets JVM options, sets the
classpath, and passes any additional command line arguments through to your
ruby script.

https://github.com/gamemachine/gamemachine/blob/master/server/bin/game_machine.sh

FYI don't use the JVM settings in that script, they are very specific to
that app and non standard.


And for reference, here is a basic gradle project that includes a plugin to
create all in one jars.   It includes gradlew which I highly recommend, and
the shadowjar plugin for creating all in one jars.  Youl can pretty much
just copy that entire directory, remove what's in src/main/java, change the
group and project name, and you have a working template to use for your own
java code.  The gradle docs are not the best when it comes to getting
started and showing the big picture, but I found gradle more intuitive then
maven when coming from ruby.

https://github.com/gamemachine/gamemachine/tree/master/server/java/agent-example

Chris


On Sun, Nov 9, 2014 at 10:41 AM, David Maxwell <[email protected]> wrote:

> I am using some custom jars in a JRuby on Rails project. All I'm doing
> is putting them into the /lib directory and then requiring java and
> importing them via my models, etc.
>
> The problem is that I want to do something more complex with these jars
> than what I'm capable of with JRuby - I need to be able to write pure
> java code to interact with them properly.
>
> So what if I want to have a central .class file or something which I
> call from my model, which then interacts with the jars? Is this
> possible? Do I need to add my custom class file to one of the jars?
>
> --
> Posted via http://www.ruby-forum.com/.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>