我有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
答案 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