我需要将我的 JRuby 应用程序编译成一个独立的 JAR 文件。我怎样才能做到这一点?
4 回答
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.
JRuby 1.6 对此进行了改进。wiki 现在有一个部分 - StandaloneJarsAndClasses - 讨论如何使用 jrubyc 生成 .class 文件和独立 jar。
使用新的编译器,您可以从 wiki 构建 .class 文件,如下例所示:
james@gealach:/tmp/fnx$ cat my_foo.rb
Foo 类
定义栏(a,b)
把 a + b
结尾
结尾
james@gealach:/tmp/fnx$ ~/jruby/bin/jrubyc --javac my_foo.rb
生成 Java 类 Foo 到 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
詹姆斯@gealach:/tmp/fnx$ ls
Foo.class Foo.java my_foo.rb
james@gealach:/tmp/fnx$ javap.exe Foo
编译自“Foo.java”
公共类 Foo 扩展 org.jruby.RubyObject{
公共静态 org.jruby.runtime.builtin.IRubyObject __allocate__(org.jruby.Ruby, org.jruby.RubyClass);
公共 Foo();
public java.lang.Object bar(java.lang.Object, java.lang.Object);
静止的 {};
}
您可以将 jar 的入口点设置为 org.jruby.JarBootstrapMain 并添加一个 jar-bootstrap.rb 文件。
只运行一个脚本:
JRuby 站点有一个可运行的 JAR 文件,您可以从JRuby 下载页面获得该文件。您想要 JRuby 完整的 JAR 文件。然后,您可以通过执行来运行您的应用程序
java -jar jruby-complete-1.4.0.jar <script>
我相信您也可以从源代码构建相同的 JAR 文件。在下载的源代码中
ant --projecthelp
要将 Ruby 脚本完全嵌入到 JAR 文件中:在 Java 中嵌入 JRuby是一个很好的起点。您可能想要解压缩jruby-complete.jar文件,添加您的 Java 主类,该类具有对 JRuby 脚本的标注或嵌入了 Ruby 代码,替换清单的主类以指向您的新入口点,然后将其 jar向上。