我找到了解决方案。您可以使用以下命令将一个java类实例添加到javascript中:
Object wrappedTest = Context.javaToJS(test, scope); ScriptableObject.putProperty(scope, "test", wrappedTest); //Where text here is name of the variable
你可以在javascript里面使用这个类的方法,即使你使用u init上下文 initSafeStandardObjects
initSafeStandardObjects
Object result = cx.evaluateString(scope, "test.sampleMethod()", "<cmd>",1, null); System.out.println(cx.toString(result));