Note that there are some explanatory texts on larger screens.

plurals
  1. POJRuby, Warbler, and Java's CLASSPATH
    text
    copied!<p>I've been developing applications in JRuby lately and really enjoying it, but I've been running into a wall when it comes to packaging my project into a JAR file when it includes external Java libraries. If the project does not depend on any external Java library JAR files, I run into no problems.</p> <p>Below is an example application. This code works perfectly fine when running the <code>./bin/my_proj</code> executable. But, when I package it into a JAR file, the external Java library cannot be loaded because it is not found on the CLASSPATH. </p> <p>When I unpackage my application's JAR file, I can see that it includes all of my code as well as the <code>vendor</code> directory containing the external Java library. So, everything's where it should be.</p> <p><strong>lib/my_proj/application.rb</strong></p> <pre><code>java_import 'com.somecompany.somejavalibrary.SomeJavaLibraryClass' module MyProj class Application &lt; SomeJavaLibraryClass # Some code implementing SomeJavaLibraryClass end end </code></pre> <p><strong>lib/my_proj.rb</strong></p> <pre><code>require 'pathname' module MyProj def root Pathname.new(__FILE__).join('..', '..').expand_path end def start setup_environment Application.new end def setup_environment @setup ||= false unless @setup @setup = true require 'java' $CLASSPATH &lt;&lt; root.join('vendor').to_s # Setup Java CLASSPATH $LOAD_PATH &lt;&lt; root.join('lib').to_s # Setup Ruby LOAD_PATH require 'some_java_library' # Load the external Java library from it's JAR require 'my_proj/application' end end extend self end </code></pre> <p><strong>bin/my_proj</strong></p> <pre><code>#!/usr/bin/env ruby $:.unshift File.expand_path( File.join('..', '..', 'lib'), __FILE__ ) require 'my_proj' MyProj.start </code></pre> <p><strong>config/warble.rb</strong></p> <pre><code>Warbler::Config.new do |config| config.features = %w(gemjar compiled) config.autodeploy_dir = 'pkg' config.dirs = %w(assets bin config lib) config.java_libs += FileList['vendor/*.jar'] end </code></pre> <p><strong>vendor/some_java_library.jar</strong></p> <pre><code># This is the external Java library </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload