(Premise) JRuby can be used ・ java can be compiled
JavaCallTest.java
)JavaCallTest.java
and prepare JavaCallTest.class
require'java'; java_import'JavaCallTest'
The following is a specific example.
JavaCallTest.java
public class JavaCallTest{
public static String str1(){
return "str1";
}
public String str2() {
return "str2";
}
}
java_call_test.rb
require 'java'
java_import 'JavaCallTest'
p JavaCallTest.str1
begin
jct1 = JavaCallTest.new
jct1.str1
rescue => e
puts e.class
puts e.message
puts e.backtrace
end
jct2 = JavaCallTest.new
p jct2.str2
begin
p JavaCallTest.str2
rescue => e
puts e.class
puts e.message
puts e.backtrace
end
$ jruby CallJava1.rb
"str1"
NoMethodError
undefined method `str1' for #<Java::Default::JavaCallTest:0x553a3d88>
Did you mean? str2
CallJava1.rb:7:in `<main>'
"str2"
NoMethodError
undefined method `str2' for Java::Default::JavaCallTest:Class
Did you mean? str1
org/jruby/RubyBasicObject.java:1657:in `method_missing'
CallJava1.rb:17:in `<main>'
It seems that the names correspond as follows
--Java static sod is a Ruby class method --Java non-static sod is a Ruby instance method
It was hard because my knowledge of Java was 0
Recommended Posts