Call Java from JRuby

Background

-See here

manner

(Premise) JRuby can be used ・ java can be compiled

  1. Write Java code (file name is JavaCallTest.java)
  2. Compile JavaCallTest.java and prepare JavaCallTest.class
  3. Within JRuby require'java'; java_import'JavaCallTest'
  4. Call appropriately

The following is a specific example.

Java code

JavaCallTest.java


public class JavaCallTest{
    public static String str1(){
        return "str1";
    }

    public String str2() {
        return "str2";
    }
}

JRuby code

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

result

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

comment

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

Call Java from JRuby
Call Kotlin's sealed class from Java
Call TensorFlow Java API from Scala
Implement Java Interface in JRuby class and call it from Java
Call Java library from C with JNI
Call GitHub API from Java Socket API part2
Call Java method from JavaScript executed in Java
Call Java methods from Nim using jnim
[Android] Call Kotlin's default argument method from Java
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Java method call from RPG (method call in own class)
Eval Java source from Java
Access API.AI from Java
From Java to Ruby !!
Use TensorFlow from JRuby
JAVA constructor call processing
Call a method with a Kotlin callback block from Java
Call a program written in Swift from Processing (Java)
Migration from Cobol to JAVA
Java starting from beginner, override
Creating ElasticSearch index from Java
New features from Java7 to Java8
Connect from Java to PostgreSQL
Java, instance starting from beginner
Java starting from beginner, inheritance
Java life starting from scratch
Using Docker from Java Gradle
From Ineffective Java to Effective Java
JavaScript as seen from Java
Execute non-Java instructions from Java
[Kotlin] Get Java Constructor / Method from KFunction and call it
Call Chain from Chain in Spring Integration
Java, abstract classes starting from beginners
Code Java from Emacs with Eclim
Deep Learning Java from scratch 6.4 Regularization
Get country from IP address (Java)
Run node.js from android java (processing)
Run a batch file from Java
[Java] Remove whitespace from character strings
Akka hands-on preparation procedure from Java
Access Teradata from a Java application
Use Chrome Headless from Selenium / Java
Java to be involved from today
Java
From Java to VB.NET-Writing Contrast Memo-
Java overload constructor starting from beginner
Work with Google Sheets from Java
Java, interface to start from beginner
Java
The road from JavaScript to Java
Reintroducing Java 8 available from Android Studio 2.4
[Java] Conversion from array to List
Call a C function from Swift
Sample code using Minio from Java
call spring management bean from pojo
Call the super method in Java
[Android / Java] Have the host Fragment (caller) call back the event from DialogFragment
Call the Microsoft Emotion API by sending image data directly from Java.
Note: Differences from Java as seen from C #
Study Deep Learning from scratch in Java.