How to Pass Command Line Arguments to Jar File

I’ve put my ruby script file, its dependencies including jruby.jar into
a single jar file. I’ve also created and compiled a ‘main’ class using
org.jruby.embed.ScriptingContainer. While I’ve got the jar to run, I
haven’t figured out how to pass command line arguments to the ruby
script (which is expecting ARGV). Anyone know how to do so? Below is my
‘main’ class.

import org.jruby.embed.ScriptingContainer;

public class Launcher {
public static void main(String[] args) {
ScriptingContainer container = new ScriptingContainer();
container.runScriptlet(“require ‘scraper’”);
}
}

Thanks.

-Dan

Hello,

On Tue, May 10, 2011 at 8:18 PM, Dan K. [email protected] wrote:

public static void main(String[] args) {
ScriptingContainer container = new ScriptingContainer();
container.runScriptlet(“require ‘scraper’”);
}
}

You can set ARGV in three ways.

  1. use setArgv() method

    container.setArgv(args);
    container.runScriptlet(“p ARGV; p ARGV.shift”);

  2. use put() method

    container.put(“ARGV”, args);
    container.runScriptlet(“p ARGV; p ARGV.shift”);

  3. directly set values to ARGV

    for (int i=0; i<args.length; i++) {
    container.runScriptlet(“ARGV << '” + args[i] + “’”);
    }
    container.runScriptlet(“p ARGV; p ARGV.shift”);

If args has only one element, performances of all three are the same.
But, there are more than one elements in args, the third way shows
worse performance than others. Choose one as you like.

-Yoko

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs