简体   繁体   中英

Can't execute a JavaScript file compiled using the Rhino JavaScript Compiler

Alright, so I created a JavaScript file named HelloWorld.js with the following contents:

java.lang.System.out.println("Hello World!");

Now, I compiled it using the Rhino JavaScript Compiler using the following command (the js.jar file is in my classpath):

java org.mozilla.javascript.tools.jsc.Main HelloWorld.js

It compiled the JavaScript file and created the Java class file as expected. Then I tried to execute the Java class file by calling java HelloWorld . However, it generated the following error message:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: HelloWorld. Program will exit.

I tried to understand what caused the java.lang.NoClassDefFoundError to be thrown, and from what I read in this blog post I learned that the java.lang.NoClassDefFoundError is thrown if a class was present during compile time but not available in Java classpath during runtime.

So I ran the javap HelloWorld command to check what the problem is, and this is what I got:

public class HelloWorld extends org.mozilla.javascript.NativeFunction implements org.mozilla.javascript.Script {
    public HelloWorld();
    public static void main(java.lang.String[]);
    public final java.lang.Object exec(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable);
    public final java.lang.Object call(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, org.mozilla.javascript.Scriptable, java.lang.Object[]);
    public int getLanguageVersion();
    public java.lang.String getFunctionName();
    public int getParamCount();
    public int getParamAndVarCount();
    public java.lang.String getParamOrVarName(int);
    public boolean getParamOrVarConst(int);
}

Now, what I understand from this is that the HelloWorld class is present and is declared as public . Hence there shouldn't be any reason why the Java Virtual Machine can't find it. This is where I'm confused. I don't know where to go from here, nor what to do to resolve this problem.

I found out that I could execute the Java class file if I invoked Rhino to call the exec method on an instance of HelloWorld as follows:

java -jar /usr/share/rhino/js.jar HelloWorld.class

However, I would like to execute the Java class file using the java HelloWorld command directly since the js.jar file already in my classpath. I would like to understand what the problem is so that I know what's really happening behind the scenes.

Are you sure the class file is on the classpath (as well as js.jar)?

Try

java -cp .;js.jar HelloWorld

(assuming HelloWorld.class in the current directory, otherwise something like -cp build;js.jar ).

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