[jruby-user] How to Pass Command Line Arguments to Jar File

35 views
Skip to first unread message

Dan King

unread,
May 10, 2011, 8:18:52 PM5/10/11
to us...@jruby.codehaus.org
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

--
Posted via http://www.ruby-forum.com/.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Yoko Harada

unread,
May 10, 2011, 9:42:01 PM5/10/11
to us...@jruby.codehaus.org
Hello,

On Tue, May 10, 2011 at 8:18 PM, Dan King <li...@ruby-forum.com> wrote:
> 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'");
>  }
> }

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

Reply all
Reply to author
Forward
0 new messages