Implement Java Interface in JRuby class and call it from Java

I wanted to write GUI in Java and processing in Ruby, so I had to handle Ruby and Java classes mutually, so note

$ tree
.
├── out
│   └── production
│       └── JRubyTest
│           └── JrubyTest
│               ├── Main.class
│               └── TestInterface.class
├── ruby
│   ├── TestInterface.jar
│   └── test_class.rb
└── src
    └── JrubyTest
        ├── Main.java
        └── TestInterface.java

TestInterface.java


package JrubyTest;

public interface TestInterface {
    java.lang.String hello();
}

TestClass.rb


java_import 'JrubyTest.TestInterface'

class TestClass
  include TestInterface
  def initialize name
    @name = name
  end

  def hello
    "hello #{@name}"
  end
end

Main.java


package JrubyTest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptContext;
import javax.script.ScriptException;

public class Main {
    public static void main(String[] args) {
        ScriptEngineManager m = new ScriptEngineManager();
        ScriptEngine rubyEngine = m.getEngineByName("jruby");
        ScriptContext context = rubyEngine.getContext();
        try {
            File file = new File("ruby/test_class.rb");
            try {
                FileReader reader = new FileReader(file);
                rubyEngine.eval(reader);
                String name = "'Taro'";
                Object testClass = rubyEngine.eval("TestClass.new " + name, context);
                if (testClass instanceof TestInterface){
                    TestInterface test = (TestInterface) testClass;
                    System.out.println(test.hello());
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}

Recommended Posts

Implement Java Interface in JRuby class and call it from Java
Write a class in Kotlin and call it in Java
Call Java from JRuby
Java method call from RPG (method call in own class)
Use of Abstract Class and Interface properly in Java
Call Kotlin's sealed class from Java
StringBuffer and StringBuilder Class in Java
[Kotlin] Get Java Constructor / Method from KFunction and call it
Call Java method from JavaScript executed in Java
Gradle automatically generates version number from git and uses it in Java
GetInstance () from a @Singleton class in Groovy from Java
How to get Class from Element in Java
Capture and save from selenium installation in Java
Generate OffsetDateTime from Clock and LocalDateTime in Java
Callable Interface in Java
abstract (abstract class) and interface (interface)
Reverse Enum constants from strings and values in Java
Call a program written in Swift from Processing (Java)
[Android development] Get an image from the server in Java and set it in ImageView! !!
[Java] Precautions when referencing a reference type in a parent class and instantiating it in a child class
Find the address class and address type from the IP address with Java
JSON in Java and Jackson Part 1 Return JSON from the server
Implement two-step verification in Java
Java class definition and instantiation
Implement Basic authentication in Java
How to call and use API in Java (Spring Boot)
Implement math combinations in Java
2 Implement simple parsing in Java
Correct the character code in Java and read from the URL
Implement Email Sending in Java
Get attributes and values from an XML file in Java
Implement functional quicksort in Java
Implement rm -rf in Java.
Implement XML signature in Java
Install the memcached plugin on MySQL and access it from Java
Mechanism and characteristics of Collection implementation class often used in Java
Implement Table Driven Test in Java 14
Difference between interface and abstract class
Call Chain from Chain in Spring Integration
Get TypeElement and TypeMirror from Class
3 Implement a simple interpreter in Java
Encoding and Decoding example in Java
[Java beginner] About abstraction and interface
Implement reCAPTCHA v3 in Java / Spring
Implement PHP implode function in Java
[JAVA] Difference between abstract and interface
Java, interface to start from beginner
Understanding equals and hashCode in Java
Try to implement Yubaba in Java
1 Implement simple lexical analysis in Java
Call TensorFlow Java API from Scala
Hello world in Java and Gradle
Call the super method in Java
[Java] Get Json from URL and handle it with standard API (javax.script)
Add the pre-built jar library to Android and call it in the framework
I wrote a Lambda function in Java and deployed it with SAM
I made a class that can use JUMAN and KNP from Java
When calling println etc. from an external Java class file in Processing
Java --Introduce CheckStyle plugin to IntelliJ IDEA and reflect it in formatter
[Java] Implement a function that uses a class implemented in the Builder pattern
Study Deep Learning from scratch in Java.