[jruby] require vs. require_relative, used from within a scriptlet

changepoint_advi.fischer at extaccount.com changepoint_advi.fischer at extaccount.com
Tue Jan 12 17:57:18 JST 2016


Here is a complete example, which demonstrates the problem on JRuby 1.7.23 and Windows 7. I replaced ScriptingContainer by IsolatedScriptingContainer, just in case this would help here too. However, this does not affect the output.

# File rdir\rapp.rb :
puts "ENTER rapp.rb"
require_relative 'rinc'

# File rdir\rinc.rb
puts 'ENTER rinc'

// File Main.java
import org.jruby.embed.PathType;
import org.jruby.embed.IsolatedScriptingContainer;
import org.jruby.embed.ScriptingContainer;
import org.jruby.embed.LocalContextScope;
public class Main {
  public static void main(String[] args){
    ScriptingContainer container  = new IsolatedScriptingContainer(LocalContextScope.CONCURRENT);
    container.runScriptlet(org.jruby.embed.PathType.CLASSPATH,"rdir/rapp.rb");
  }
}

:: Batch script to compile and run the example:
setlocal
set RJAR=c:\dl\jruby-complete-1.7.23.jar
:: Compile
set CLASSPATH=.;%RJAR%
javac -d . Main.java
@if ERRORLEVEL 1 goto :eof
:: Make jar
del app.jar 2>NUL
jar cvfm app.jar MANIFEST.MF -C . Main.class rdir
:: Deploy
set RUNDIR=C:\temp\jr12run
mkdir %RUNDIR% 2>NUL
copy app.jar %RUNDIR%
:: Run
cd %RUNDIR%
@if ERRORLEVEL 1 goto :eof
java -cp app.jar;%RJAR% Main

This results in the following output:

io/console not supported; tty will not be manipulated
ENTER rapp.rb
LoadError: no such file to load -- classpath:C:/rdir/rinc
           require at org/jruby/RubyKernel.java:1040
           require at uri:classloader:/META-INF/jruby.home/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:54
  require_relative at uri:classloader:/jruby/kernel19/kernel.rb:13
            (root) at classpath:/rdir/rapp.rb:2
Exception in thread "main" org.jruby.embed.EvalFailedException: (LoadError) no such file to load -- classpath:C:/rdir/rinc
        at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:132)
        at org.jruby.embed.ScriptingContainer.runUnit(ScriptingContainer.java:1353)
        at org.jruby.embed.ScriptingContainer.runScriptlet(ScriptingContainer.java:1398)
        at Main.main(Main.java:9)
Caused by: org.jruby.exceptions.RaiseException: (LoadError) no such file to load -- classpath:C:/rdir/rinc
        at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:1040)
        at RUBY.require(uri:classloader:/META-INF/jruby.home/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:54)
        at RUBY.require_relative(uri:classloader:/jruby/kernel19/kernel.rb:13)
        at RUBY.(root)(classpath:/rdir/rapp.rb:2)

From: JRuby [mailto:jruby-bounces at ruby-lang.org] On Behalf Of christian
Sent: Monday, January 11, 2016 5:36 PM
To: JRuby project mailing list
Subject: Re: [jruby] require vs. require_relative, used from within a scriptlet

this should actually work. which jruby version are you using ?

On Mon, Jan 11, 2016 at 3:11 PM, <changepoint_advi.fischer at extaccount.com<mailto:changepoint_advi.fischer at extaccount.com>> wrote:
I'm running my JRuby application via a ScriptingContainer. Everything is in a Jar File, and the Ruby source files are all within the same subdirectory in the jar. Seen from the jar file as the base directory, by ruby files would be rdir/scriptlet.rb and rdir/application.rb. The scriptlet is called by

    container.runScriptlet(org.jruby.embed.PathType.CLASSPATH,"rdir/scriptlet.rb");

Within scriptlet.rb, I would like to require application.rb. I can do this in the following way:

    require 'rdir/application' # This works

I can NOT do this in the following way:

    require_relative 'application" # This does not work

I had expected, that both would work. require_relative looks up the included file relative to the directory of the including file, which is in both cases 'rdir'. Why can't I use require_relative here? Does 'require_relative' only work in this way, if it is called from within a file which itself is being processed by a 'require'/'require_relative' command?

I would prefer not having 'rdir' hardcoded in my file, and therefore would like to use require_relative, if possible.

Ronald

If you are not the addressee, please inform us immediately that you have received this e-mail by mistake, and delete it. We thank you for your support.


If you are not the addressee, please inform us immediately that you have received this e-mail by mistake, and delete it. We thank you for your support.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ruby-lang.org/pipermail/jruby/attachments/20160112/e18a6f0f/attachment-0001.html>


More information about the JRuby mailing list