class Rake::JavaExtensionTask

def java_classpath_args


of ways to discover the Java classpath correctly.
Copied verbatim from the ActiveRecord-JDBC project. There are a small myriad

Discover the Java/JRuby classpath and build a classpath arguments
def java_classpath_args
  jruby_cpath = nil
  if RUBY_PLATFORM =~ /java/
    begin
      cpath  = Java::java.lang.System.getProperty('java.class.path').split(File::PATH_SEPARATOR)
      cpath += Java::java.lang.System.getProperty('sun.boot.class.path').split(File::PATH_SEPARATOR)
      jruby_cpath = cpath.compact.join(File::PATH_SEPARATOR)
    rescue
    end
  end
  # jruby_cpath might not be present from Java-9 onwards as it removes
  # sun.boot.class.path. Check if JRUBY_HOME is set as env variable and try
  # to find jruby.jar under JRUBY_HOME
  unless jruby_cpath
    jruby_home = ENV['JRUBY_HOME']
    if jruby_home
      candidate = File.join(jruby_home, 'lib', 'jruby.jar')
      jruby_cpath = candidate if File.exist?(candidate)
    end
  end
  # JRUBY_HOME is not necessarily set in JRuby-9.x
  # Find the libdir from RbConfig::CONFIG and find jruby.jar under the
  # found lib path
  unless jruby_cpath
    libdir = RbConfig::CONFIG['libdir']
    if libdir.start_with?("uri:classloader:")
      raise 'Cannot build with jruby-complete from Java 9 onwards'
    end
    candidate = File.join(libdir, "jruby.jar")
    jruby_cpath = candidate if File.exist?(candidate)
  end
  unless jruby_cpath
    raise "Could not find jruby.jar. Please set JRUBY_HOME or use jruby in rvm"
  end
  if @classpath and @classpath.size > 0
    jruby_cpath = [jruby_cpath, *@classpath].join(File::PATH_SEPARATOR)
  end
  ["-cp", jruby_cpath]
end