从Nashron JavaScript运行Java - 异常java.lang.ClassNotFoundException

时间:2014-12-08 10:25:35

标签: java javascript nashorn nodeclipse

我有runMyJava.jjs(使用Nashorn在JavaScript中运行Java)

var javaClass = Java.type('mypackage.MyJavaClass'); 
javaClass.hello();

运行

package mypackage;

public class MyJavaClass {

    public static void main(String[] args) {
        System.out.println("hello from MyJavaClass.main ");

    }

}

但我得到

Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: mypackage.MyJavaClass
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:382)
    at jdk.nashorn.tools.Shell.apply(Shell.java:381)
    at jdk.nashorn.tools.Shell.runScripts(Shell.java:310)
    at jdk.nashorn.tools.Shell.run(Shell.java:166)
    at jdk.nashorn.tools.Shell.main(Shell.java:130)
    at jdk.nashorn.tools.Shell.main(Shell.java:109)
Caused by: java.lang.ClassNotFoundException: mypackage.MyJavaClass
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)

从Eclipse / Enide Studio或终端jjs runJava.jjs

运行时

所有项目都在https://github.com/paulvi/JavaRoot

1 个答案:

答案 0 :(得分:0)

我应该运行传递classpath

jjs -cp ./ runJava.jjs

请参阅http://docs.oracle.com/javase/8/docs/technotes/tools/unix/jjs.html

并且Java应该已经编译了

javac *.java

how to access methods from my class inside javascript in Nashorn