本文介绍了Java Nashorn-ClassNotFoundException-Java.type()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在为Bukkit-Server创建一个插件,但是使用Nashorn脚本引擎时遇到了问题。我正在从Java-Plugin中评估外部javascript文件。我无法从自己的插件中导入JavaScript,只能使用标准的Java类(例如 var JavaBool = Java.type('java.lang.Boolean'); ,但不是 var Holder = Java.type('io.github.advtes​​t1.js.JSHolder');

I am currently creating a plugin for the Bukkit-Server, but i have a problem using the Nashorn scripting engine. I am evaluating the external javascript file from my Java-Plugin. I cant get my javascript to import the classes from my plugin, only standard java classes are working (like var JavaBool = Java.type('java.lang.Boolean');, but not var Holder = Java.type('io.github.advtest1.js.JSHolder');)

每当我尝试加载其中一个时,都会出现以下错误:

Whenever I try to load one of these I get the following error:

Caused by: java.lang.ClassNotFoundException: io.github.advtest1.js.JSHolder

经过一番研究,我发现它有事可做与我的插件类蜂在类路径中,但是当Bukkit本身加载插件并且我不希望服务器有任何其他启动选项时,我如何将其添加到类路径中,然后 java -jar bukkit.jar

After a bit of researching i found that it has something to do with my plugin-classes beeing in the classpath, but how can i add it to the classpath when Bukkit itself loads the plugin and i don't want any other startoptions for the server then java -jar bukkit.jar?

如果您需要其他信息,可以随时询问。
预先感谢!

Feel free to ask, if you need additional information.Thanks in advance!

推荐答案


  • Nashorn使用发现的线程上下文加载器在创建引擎时从Java.type API查找Java类。

    • Nashorn uses the thread context loader found at the time of engine creation to find Java classes from Java.type API.

      如果您使用 -classpath nashorn命令行选项,Nashorn也将使用另一个加载程序。 Nashorn尝试使用使用指定的类路径初始化的引擎创建过程中创建的新加载器加载类。请注意,可以通过 nashorn.args Java System属性传递nashorn命令行选项。因此,如果在其他地方(例如,在您的配置中)指定-Dnashorn.args =-classpath your_path,则Nashorn可以从指定的your_path访问类。

      Nashorn also uses another loader if you use "-classpath" nashorn command line option. Nashorn tries to load classes using a fresh loader created during engine creation initialized with that classpath specified. Note that nashorn command line options can be passed via "nashorn.args" Java System property. So, if you specific -Dnashorn.args="-classpath your_path" elsewhere (say, in your config), then Nashorn can access classes from that your_path specified.

      如果无法通过System属性(或在使用 jjs工具的情况下通过命令行)传递Nashorn引擎选项,则可以将线程上下文加载器设置为较早的建议的合适加载器答案。

      If cannot pass Nashorn engine options via System property [or via command line in the case "jjs" tool use], you can set thread context loader to be appropriate loader as suggested by an earlier answer.

      如果由于其他应用程序依赖性而不希望这样做,则可以获取所需类的java.lang.Class对象,并将其作为变量公开给脚本(从Java代码中,您可以获取Class对象并调用ScriptEngine.put方法)。然后,脚本可以访问该属性的静态属性以获取类型对象。对于类型对象,通常的新,静态方法调用等均按预期工作。

      If that is not desired because of other application dependencies, you get java.lang.Class object of the desired class and expose the same as variable to the script (from your Java code, you could get Class object and call ScriptEngine.put method). The script then can access "static" property on that to get 'type' object. With type object, usual "new", static method calls etc. work as expected.

      示例:

      import javax.script.*;
      
      public class Main {
         public static void main(String[] args) throws Exception {
             ScriptEngineManager m = new ScriptEngineManager();
             ScriptEngine e = m.getEngineByName("nashorn");
             e.put("Vec", java.util.Vector.class); // replace it with any Class object
             e.eval("var T = Vec.static; var obj = new T()"); // create new Vector
             e.eval("print(obj.getClass())");
         }
      }
      

      这篇关于Java Nashorn-ClassNotFoundException-Java.type()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:34