Create deployable JRuby JAR file?
I need to compile my JRuby application into a standalone JAR file. H开发者_开发百科ow can I do this?
Warbler 1.3.0 or newer can also be used to make an executable JAR file.
Quick instructions. Make sure you're using JRuby`s gem, etc. here:
$ gem install warbler
$ mkdir bin
$ cat <<EOF > bin/myapp
#!/usr/bin/env jruby
puts "Hello World"
EOF
$ chmod a+x bin/myapp
$ warble jar
You should now have a myapp.jar
file in the current directory. According to the README
you just add any libraries you need in the lib
directory and use either (or both) a .gemspec
or Gemfile
to control any other gems that need to be put into the .jar
file.
Check out rawr. It is a gem that packages your application in a JAR file.
If you want to compile for source code protection you must produce a .class
file for each script in your project (as described here) and then replace the contents of the original script with
require 'the_corresponding_class_file'
for each of the compiled scripts.
JRuby 1.6 improved this. There's now a section of the wiki - StandaloneJarsAndClasses - that talks about how to use jrubyc to generate .class files and standalone jars.
With the new compiler, you can build .class files like this example from the wiki:
james@gealach:/tmp/fnx$ cat my_foo.rb class Foo def bar(a, b) puts a + b end end james@gealach:/tmp/fnx$ ~/jruby/bin/jrubyc --javac my_foo.rb Generating Java class Foo to C:/cygwin/tmp/fnx/Foo.java javac -d C:/cygwin/tmp/fnx -cp C:/cygwin/home/james/jruby/lib/jruby.jar;. C:/cygwin/tmp/fnx/Foo.java james@gealach:/tmp/fnx$ ls Foo.class Foo.java my_foo.rb james@gealach:/tmp/fnx$ javap.exe Foo Compiled from "Foo.java" public class Foo extends org.jruby.RubyObject{ public static org.jruby.runtime.builtin.IRubyObject __allocate__(org.jruby.Ruby, org.jruby.RubyClass); public Foo(); public java.lang.Object bar(java.lang.Object, java.lang.Object); static {}; }
And you can set the entrypoint for a jar to org.jruby.JarBootstrapMain and add a jar-bootstrap.rb file.
To just run a script:
The JRuby site has a runnable JAR file that you can get at the JRuby download page. You want the JRuby complete JAR file. You can then run your application by doing
java -jar jruby-complete-1.4.0.jar <script>
I believe you can also build the same JAR file from source. In the downloaded source do
ant --projecthelp
To embed a Ruby script completely into a JAR file: Embedding JRuby in Java is a good starting point. You will probably want to unjar the jruby-complete.jar
file, add your Java main class that either has the a callout to a JRuby script or has the Ruby code embedded in it, replace the manifest's main class to point to your new entry point, and jar it back up.
精彩评论