Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hmistry opened this issue Jun 24, 2021 · 1 comment

Comments

@hmistry
Copy link

hmistry commented Jun 24, 2021

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@hmistry and others