Skip to content

Jar file loaderror, cannot open library 'uri:classloader://myapp/path/to/file'  #506

@hmistry

Description

@hmistry

I have a CLI gem called myapp that uses a C shared library (some_lib.so) which is interfaced with ruby code using ffi gem. The library is loaded in a Ruby module using:

ffi_lib File.join(File.dirname(ENV["BUNDLE_GEMFILE"] || "./"), "ext", "some_lib.so") 

This works in development but not when the gem is packaged with warbler into a "runnable" jar file. I've tried various paths but it either resolves to the host system path which won't work on another system or errors out with file loaderror, cannot open library 'uri:classloader://myapp/path/to/some_lib.so'.

I verified the library some_lib.so is packaged into the jar file under the correct subfolders myapp/ext/some_lib.so along with the rest of the gem files in their respective folders.

It seems like it is not finding the library within the internal packaged files. Do you have any thoughts on what's going on and how I can get ffi_lib to load the file that is packaged in the jar file?

Error backtrace:

USHQ0000:projects hm$ java -jar myapp.jar t

# These resolve to host system file paths which won't work on another system
# puts "Dirwd: #{Dir.getwd}"
# puts "Dirpwd: #{Dir.pwd}"
# puts "Dir: #{__dir__}"
# puts "File: #{__FILE__}"
# puts "File_exp: #{File.expand_path(__FILE__)}"

Dirpwd: /Users/hm/Documents/projects
Dir: /Users/hm/Documents/projects/myapp/lib/myapp
File: /Users/hm/Documents/projects/myapp/lib/myapp/some_lib.rb
File_exp: /Users/hm/Documents/projects/myapp/lib/myapp/some_lib.rb

# puts ENV["GEM_HOME"]
# puts ENV["BUNDLE_GEMFILE"]  # This seems like an internal path within the jar file... correct?

uri:classloader:/
uri:classloader://myapp/Gemfile

LoadError: Could not open library 'uri:classloader://myapp/ext/some_lib.so' : dlopen(uri:classloader://myapp/ext/some_lib.so, 5): image not found
            ffi_lib at uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/ffi/library.rb:114
                map at org/jruby/RubyArray.java:2588
            ffi_lib at uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/ffi/library.rb:84
  <module:XenaSflm> at /Users/hm/Documents/projects/myapp/lib/myapp/some_lib.rb:28
  <module:Dnatools> at /Users/hm/Documents/projects/myapp/lib/myapp/some_lib.rb:6
             <main> at /Users/hm/Documents/projects/myapp/lib/myapp/some_lib.rb:5
            require at org/jruby/RubyKernel.java:974
   require_relative at org/jruby/RubyKernel.java:1002
             <main> at /Users/hm/Documents/projects/myapp/lib/myapp/sflm.rb:3
            require at org/jruby/RubyKernel.java:974
   require_relative at org/jruby/RubyKernel.java:1002
             <main> at /Users/hm/Documents/projects/myapp/lib/myapp.rb:30
            require at org/jruby/RubyKernel.java:974
   require_relative at org/jruby/RubyKernel.java:1002
             <main> at myapp/bin/myapp:5
               load at org/jruby/RubyKernel.java:1009
             <main> at uri:classloader:/META-INF/main.rb:1
            require at org/jruby/RubyKernel.java:974
            require at uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:83
             <main> at <script>:3
ERROR: org.jruby.embed.EvalFailedException: (LoadError) Could not open library 'uri:classloader://myapp/ext/some_lib.so' : dlopen(uri:classloader://myapp/ext/some_lib.so, 5): image not found

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions