Call Java method from JavaScript executed in Java

■ Original document (1) https://docs.oracle.com/javase/jp/6/technotes/guides/scripting/programmer_guide/ ■ Original document (2) https://developer.mozilla.org/ja/docs/Rhino/Scripting_Java


package pkg;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class HelloJavaScript {

	public void run() {

		System.err.println("hello!");

	}

	public static void main(String[] args) {

		// see
		// https://developer.mozilla.org/ja/docs/Rhino/Scripting_Java
		// These packages are likely not in the java package, so you'll need to
		// prefix the package name with "Packages."

		// Get the JavaScript engine
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("JavaScript");

		String script = "" //
				+ "var obj = new Packages.pkg.HelloJavaScript();" //
				+ "obj.run();" //
		;

		try {
			engine.eval(script);
		} catch (ScriptException e) {
			e.printStackTrace();
		}

	}

}

Recommended Posts

Call Java method from JavaScript executed in Java
Java method call from RPG (method call in own class)
Call the super method in Java
Using JavaScript from Java in Rhino 2021 version
Call Java from JRuby
[Android] Call Kotlin's default argument method from Java
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Try calling JavaScript in Java
Call a method with a Kotlin callback block from Java
Call a program written in Swift from Processing (Java)
JavaScript as seen from Java
Implement Java Interface in JRuby class and call it from Java
[Kotlin] Get Java Constructor / Method from KFunction and call it
Call Chain from Chain in Spring Integration
Call Kotlin's sealed class from Java
Automatic photo resizing method in Java
Java, JavaScript, C # (difference in assignment)
The road from JavaScript to Java
Call TensorFlow Java API from Scala
Java method
java (method)
Java method
[Java] method
[Java] method
Call Java library from C with JNI
Call GitHub API from Java Socket API part2
OCR in Java (character recognition from images)
Call the Windows Notification API in Java
Concurrency Method in Java with basic example
Reverse Key from Value in Java Map
Use "Rhino" which runs JavaScript in Java
What is the main method in Java?
Call Java methods from Nim using jnim
Get history from Zabbix server in Java
Call Visual Recognition in Watson Java SDK
Element operation method in appium TIPS (Java)
[Java] Pass arguments to constructor in Mockito / Set method default call to callRealMethod
JSON in Java and Jackson Part ③ Embed JSON in HTML and use it from JavaScript
GetInstance () from a @Singleton class in Groovy from Java
[Java] Handling of JavaBeans in the method chain
[Java] Dynamic method call by reflection of enum (enum)
Partization in Java
Changes in Java 11
Java8 method reference
I tried hitting a Java method from ABCL
Rock-paper-scissors in Java
Differences in writing Java, C # and Javascript classes
How to get Class from Element in Java
Text extraction in Java from PDF with pdfbox-2.0.8
[Java] forEach method
Method name of static factory method learned from Java 8
Capture and save from selenium installation in Java
Get unixtime (seconds) from ZonedDateTime in Scala / Java
Java and JavaScript
[Deep Learning from scratch] in Java 3. Neural network
Method name of method chain in Java Builder + α
java8 method reference
Pi in Java
Check method call arguments in blocks with RSpec
[Java] Random method
[Java] split method