我想使用 Nashorn 控制台来替代 Rails c。例如,我想调用 Java 方法从远程系统导入数据并执行数据迁移。我发现这很有趣:
https://www.baeldung.com/java-nashorn
$JAVA_HOME/bin/jjs
jjs> print("test");
test
jjs>
例如,我如何从部署在 Wildfly 服务器上的 WAR 包调用一些 Java 方法并传递一些参数?
您能提出更好的替代方案吗?
最佳答案
引用 Oracle 的“Java Scripting Programmer's Guide”第 3 章,“Using Java From Scripts”:
To access primitive and reference Java types from JavaScript, call the Java.type() function, which returns a type object that corresponds to the full name of the class passed in as a string. The following example shows you how to get various type objects:
var ArrayList = Java.type("java.util.ArrayList"); var intType = Java.type("int"); var StringArrayType = Java.type("java.lang.String[]"); var int2DArrayType = Java.type("int[][]");
The type object returned by the Java.type() function can be used in JavaScript code similar to how a class name is used in Java. For example, you can can use it to instantiate new objects as follows:
var anArrayList = new Java.type("java.util.ArrayList");
尽管您的问题对于您到底想做什么有点含糊。如果您在您的应用程序中使用 Nashorn,您使用它执行的脚本将有权访问您的应用程序所使用的 Java 类。
关于java - 如何使用Nashorn引擎调用Java对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52399749/