简体   繁体   中英

import class file in Jruby

I have several scripts written in Jruby that I would like to obfuscate by converting them to .class files. But I am running into an error when I tried to access the .class file using Jruby.

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

jrubyc Test2.rb

which produce the following .class files :

Test2.class

Now, I trying to access Test2 and its methods from another jruby file, action.rb

    #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. Given the file 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) :

If you pass the --java option to jrubyc, it will generate a .java file instead of a .class file. You can then fall back on familiar Java tools to finish the job.

$ 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 :

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 :

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 :

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 :

$ 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 :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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