创建可部署的 JRuby JAR 文件?

发布于 2024-08-21 20:05:32 字数 42 浏览 3 评论 0原文

我需要将 JRuby 应用程序编译成独立的 JAR 文件。我该怎么做?

I need to compile my JRuby application into a standalone JAR file. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流殇 2024-08-28 20:05:32

Warbler 1.3.0 或更高版本也可用于制作可执行 JAR 文件。

快速说明。确保您在此处使用 JRuby`s gem 等:

$ gem install warbler
$ mkdir bin
$ cat <<EOF > bin/myapp
#!/usr/bin/env jruby

puts "Hello World"

EOF
$ chmod a+x bin/myapp
$ warble jar

您现在应该在当前目录中有一个 myapp.jar 文件。根据README,您只需在lib目录中添加所需的任何库,并使用.gemspec之一(或两者) Gemfile 来控制需要放入 .jar 文件中的任何其他 gem。

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.

鲜肉鲜肉永远不皱 2024-08-28 20:05:32

查看 rawr。它是一个将您的应用程序打包到 JAR 文件中的 gem。

如果要进行编译以保护源代码,则必须为项目中的每个脚本生成一个 .class 文件(如

require 'the_corresponding_class_file'

/JRubyCompiler" rel="nofollow noreferrer">此处),然后将每个编译脚本

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.

甜味超标? 2024-08-28 20:05:32

JRuby 1.6 改进了这一点。现在 wiki 的一个部分 - StandaloneJarsAndClasses - 讨论如何使用 jrubyc 生成.class 文件和独立 jar。

使用新的编译器,您可以构建 .class 文件,如 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 {};
}

您可以将 jar 的入口点设置为 org.jruby.JarBootstrapMain 并添加 jar-bootstrap.rb 文件。

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.

花之痕靓丽 2024-08-28 20:05:32

仅运行脚本:

JRuby 站点有一个可运行的 JAR 文件,您可以从 JRuby 获取该 文件下载页面。您需要 JRuby 完整的 JAR 文件。然后您可以通过执行以下操作来运行您的应用程序,

java -jar jruby-complete-1.4.0.jar <script>

我相信您也可以从源代码构建相同的 JAR 文件。在下载的源代码中,将

ant --projecthelp

Ruby 脚本完全嵌入到 JAR 文件中:在 Java 中嵌入 JRuby 是一个很好的起点。您可能需要解压 jruby-complete.jar 文件,添加具有 JRuby 脚本标注或嵌入 Ruby 代码的 Java 主类,替换清单的主类指向您的新入口点,并将其备份。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文