简体   繁体   English

在Jruby中导入类文件

[英]import class file in Jruby

I have several scripts written in Jruby that I would like to obfuscate by converting them to .class files. 我有一些用Jruby编写的脚本,希望通过将它们转换为.class文件来进行混淆。 But I am running into an error when I tried to access the .class file using Jruby. 但是,当我尝试使用Jruby访问.class文件时,我遇到了错误。

My file structure looks like this: 我的文件结构如下所示:

temp/action.rb
temp/src/Test2.rb

#temp/src/Test2.rb

class Test2
    def self.add(num1,num2)
        return num1 + num2
    end
end

I converted Test2.rb to .class files using 我使用将Test2.rb转换为.class文件

jrubyc Test2.rb

which produce the following .class files : 产生以下.class文件:

Test2.class

Now, I trying to access Test2 and its methods from another jruby file, action.rb 现在,我尝试从另一个jruby文件action.rb访问Test2及其方法。

    #temp/action.rb

    require 'java'

    $CLASSPATH << "src"

    java_import 'Test2'

    t = Test2.new
    puts t.add(2,3)

But I am getting the following error: 但是我收到以下错误:

NoMethodError: undefined method 'add' for <Java::Default::Test2:0x369f73a2>

I'm not sure what I am doing wrong, any help would be appreciated. 我不确定自己在做什么错,任何帮助将不胜感激。

I haven't used JRuby for five years and discover strange things. 我已经五年没有使用JRuby了,发现了很多奇怪的东西。 Given the file Test2.rb : 给定文件Test2.rb

class Test2
    def self.addc(num1, num2)
        num1 + num2
    end

    def self.xyz
        puts 'xyz'
    end

    def addi(num1, num2)
        num1 + num2
    end
end

puts Test2.addc(4,6)

t = Test2.new
puts t.addi(2, 3)

which I compile : 我编译:

$ jrubyc Test2.rb 
$ jruby -J-cp .:/Users/b/.rvm/rubies/jruby-9.0.5.0/lib/jruby.jar use_t.rb 
in use_t.rb
NoMethodError: undefined method `addc' for Java::Default::Test2:Class

Then I read in Using Jruby (from Pragmatic Programmers, but no longer available) : 然后,我读了《使用Jruby》(来自实用程序员,但不再可用):

If you pass the --java option to jrubyc, it will generate a .java file instead of a .class file. 如果将--java选项传递给jrubyc,它将生成一个.java文件而不是.class文件。 You can then fall back on familiar Java tools to finish the job. 然后,您可以依靠熟悉的Java工具来完成工作。

$ jrubyc --java Test2.rb
$ javac -cp .:/Users/b/.rvm/rubies/jruby-9.0.5.0/lib/jruby.jar Test2.java 
Test2.java:16: error: unclosed string literal
        String source = new StringBuilder("class Test2
                                          ^
Test2.java:17: error: illegal character: '\'
\n" +
^

I open Test2.java and see : 我打开Test2.java并看到:

public class Test2 extends RubyObject  {
    private static final Ruby __ruby__ = Ruby.getGlobalRuntime();
    private static final RubyClass __metaclass__;

    static {
        String source = new StringBuilder("class Test2
\n" +
            "    def self.addc(num1, num2)
\n" +
            "        num1 + num2
\n" +
            "    end
\n" +
...

I edit this file to "repair" it like so : 我编辑此文件以“修复”它,如下所示:

static {
    String source = new StringBuilder("class Test2\n" +
        "    def self.addc(num1, num2)\n" +
        "        num1 + num2\n" +
        "    end\n" +
        "    \n" +
        "    def self.xyz\n" +
        "        puts 'xyz'\n" +
        "    end\n" +
        "\n" +
...

Then recompile 然后重新编译

$ javac -cp .:/Users/b/.rvm/rubies/jruby-9.0.5.0/lib/jruby.jar Test2.java 

and, with file use_t.rb : 并且,与文件use_t.rb

require 'java'

#$CLASSPATH << '.'
java_import 'Test2'

puts "in #{__FILE__}"

Test2.xyz

puts ::Test2.addc(8,9)

t = ::Test2.new
puts t.addi(2,3)

execute : 执行 :

$ jruby -J-cp .:/Users/b/.rvm/rubies/jruby-9.0.5.0/lib/jruby.jar use_t.rb 
10
5
uri:classloader:/jruby/java/core_ext/object.rb:93: warning: already initialized constant Test2
in use_t.rb
xyz
17
5

It works ! 有用 ! Strange detour. 奇怪的弯路。

It's not a problem of coercing parameters, because xyz alone : 这不是强制参数的问题,因为仅xyz

NoMethodError: undefined method `xyz' for Java::Default::Test2:Class

Then I try another version : 然后我尝试另一个版本:

$ rvm install jruby
...
jruby-9.1.7.0 - #configure
jruby-9.1.7.0 - #download
...
jruby-9.1.7.0 - #generating default wrappers........
$ rvm use jruby
Using /Users/b/.rvm/gems/jruby-9.1.7.0

Compile again : 再次编译:

$ jrubyc Test2.rb 
TypeError: failed to coerce org.objectweb.asm.ClassWriter to org.jruby.org.objectweb.asm.ClassVisitor
  block in compile_files_with_options at /Users/b/.rvm/rubies/jruby-9.1.7.0/lib/ruby/stdlib/jruby/compiler.rb:189

Then I download the latest release ( jruby-complete-9.1.15.0.jar ) from jruby.org and, with this error, I find in a forum that somebody used the following command and try it : 然后,我从jruby.org下载了最新版本( jruby-complete-9.1.15.0.jar ),由于出现此错误,我在一个论坛中发现有人使用以下命令并进行尝试:

$ java -cp .:/userdata/Sources/jruby-complete-9.1.15.0.jar org.jruby.Main ./use_t.rb  
10
5
uri:classloader:/jruby/java/core_ext/object.rb:95: warning: already initialized constant Test2
in ./use_t.rb
xyz
17
5

It works, but don't ask why :( 它有效,但不要问为什么:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM