Unable to execute JavaScript file compiled using Rhino JavaScript compiler

Ok, so I created a JavaScript file called 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 class path):

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

He compiled a JavaScript file and created a Java class file, as expected. Then I tried to execute the Java class file by calling java HelloWorld . However, he created 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 thrown, and from what I read in this blog post, I found out that java.lang.NoClassDefFoundError is raised if the class was present at compile time but not available in the Java classpath at 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 I understand that the HelloWorld class is present and declared as public . Therefore, there should be no reason why the Java virtual machine cannot find it. Here I am confused. I do not know where to go from here, and what to do to solve this problem.

I found out that I can execute the Java class file if I called Rhino to call the exec method on the HelloWorld instance 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 from the js.jar file already in my class path. I would like to understand what the problem is, so that I know what really happens behind the scenes.

+4
source share
1 answer

Are you sure the class file is in 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 ).

+3
source

Source: https://habr.com/ru/post/1400807/


All Articles