简体   繁体   中英

Running Java from Nashron JavaScript - Exception java.lang.ClassNotFoundException

I have runMyJava.jjs (to run Java from within JavaScript using Nashorn)

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

to run

package mypackage;

public class MyJavaClass {

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

    }

}

but I get

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)

when running from Eclipse/Enide Studio or within terminal jjs runJava.jjs

All project is at https://github.com/paulvi/JavaRoot

I should have run passing classpath

jjs -cp ./ runJava.jjs

see http://docs.oracle.com/javase/8/docs/technotes/tools/unix/jjs.html

and the Java should be already compiled

javac *.java

how to access methods from my class inside javascript in Nashorn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM