Setting java.library.path in JRuby

View: New views
2 Messages — Rating Filter:   Alert me  

Setting java.library.path in JRuby

by Simon Chiang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm attempting to use a JNI provided by somebody else (CLIPS).  Basically I need to require a jar and then include a Java class into my JRuby environment.  When my folder is setup like this it all works:

  script.rb
  CLIPSJNI.jar
  libCLIPSJNI.jnilib

  [script.rb]
  include Java
  require 'CLIPSJNI.jar'
  include_class 'CLIPSJNI.Environment'
  puts Environment

  % jruby script.rb
  Java::CLIPSJNI::Environment

However when I setup like this I get an error:

  script.rb
  lib/
    CLIPSJNI.jar
    libCLIPSJNI.jnilib

  % jruby script.rb
  (eval):1:in `include_class': cannot link Java class CLIPSJNI.Environment, probable missing dependency: no
  CLIPSJNI in java.library.path (NameError)

I can fix it by setting java.library.path from the command line:

  % jruby -J-Djava.library.path=lib clips.rb
  Java::CLIPSJNI::Environment

So how can I set java.library.path within my script so I don't need the command line flag?  I've tried:

  current = java.lang.System.getProperty("java.library.path")
  java.lang.System.setProperty("java.library.path", "#{current}:#{File.expand_path('lib')}");

And:

  java.lang.System.load(File.expand_path('lib/libCLIPSJNI.jnilib'));

No luck so far.  Any ideas?  Thanks.

Re: Setting java.library.path in JRuby

by Antti Karanta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Wed, 04 Nov 2009 18:43:21 +0200, Simon Chiang  
<simon.a.chiang@...> wrote:

> So how can I set java.library.path within my script so I don't need the
> command line flag?

   I don't think you can. Like many other system properties (e.g.  
java.class.path), the jvm copies the value at startup and ignores any  
changes thereafter. There's nothing jruby can do about it.


> No luck so far.  Any ideas?  Thanks.

   These things come to mind:

   Add the folder containing the native library to PATH (if on windows) or  
to LD_LIBRARY_PATH (e.g. linux, solaris) before launching your script.

   Make a short shell script to launch your script.
   As a variation of this, you could make your ruby script spawn a copy of  
itself in a new process if it notices java.library.path does not contain  
the folder you want (setting java.library.path correctly for the new  
process, naturally). Note that there are some subtleties about launching a  
new jruby process from jruby (IIRC there's an optimization that prevents a  
new process from being created and instead runs the script in the same  
process) - others more knowledgeable can comment more on this.



       ::Antti::



 


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email