好的,所以我创建了一个名为test.js
的JavaScript文件,其中包含以下代码:
print("It works!");
我使用Rhino JavaScript编译器编译它没有任何错误。然后我创建了一个名为foo.js
的新文件,其中包含以下代码:
var test = require("./test.class");
现在,当我在Ringo中运行foo.js
时抛出以下异常和堆栈跟踪:
Uncaught exception:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.ringojs.tools.launcher.Main.run(Main.java:66)
at org.ringojs.tools.launcher.Main.main(Main.java:45)
Caused by: java.lang.NoClassDefFoundError: /home/aaditmshah/test (wrong name: test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at org.mozilla.javascript.DefiningClassLoader.defineClass(DefiningClassLoader.java:62)
at org.ringojs.engine.ClassModuleLoader.load(ModuleLoader.java:126)
at org.ringojs.engine.ReloadableScript.compileScript(ReloadableScript.java:153)
at org.ringojs.engine.ReloadableScript.getScript(ReloadableScript.java:118)
at org.ringojs.engine.ReloadableScript.exec(ReloadableScript.java:227)
at org.ringojs.engine.ReloadableScript.load(ReloadableScript.java:215)
at org.ringojs.engine.RingoWorker.loadModuleInternal(RingoWorker.java:283)
at org.ringojs.engine.Require.call(Require.java:81)
at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97)
at org.mozilla.javascript.gen._home_aaditmshah_foo_js_3._c_script_0(/home/aaditmshah/foo.js:1)
at org.mozilla.javascript.gen._home_aaditmshah_foo_js_3.call(/home/aaditmshah/foo.js)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:426)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3178)
at org.mozilla.javascript.gen._home_aaditmshah_foo_js_3.call(/home/aaditmshah/foo.js)
at org.mozilla.javascript.gen._home_aaditmshah_foo_js_3.exec(/home/aaditmshah/foo.js)
at org.ringojs.engine.ReloadableScript.evaluate(ReloadableScript.java:186)
at org.ringojs.engine.RingoWorker.evaluateScript(RingoWorker.java:315)
at org.ringojs.engine.RhinoEngine.runScript(RhinoEngine.java:186)
at org.ringojs.tools.RingoRunner.run(RingoRunner.java:152)
... 6 more
enter code here
我不知道哪里出错了。我有最新版本的Ringo和Rhino 1.7R3。我将当前目录添加到我的类路径中,方法是将以下行添加到foo.js
:
addToClasspath(module.resolve("."));
但是,它仍会产生相同的错误。我不知道如何做这项工作。任何帮助将不胜感激。
答案 0 :(得分:2)
好的,我仔细查看了堆栈跟踪,并意识到它正确地找到了类文件,它只是尝试从错误的包(home.aaditmshah.test
而不是test
)加载它。如果类文件位于模块路径中,则可以仅使用模块(类)名称正确加载它。默认情况下,当前目录不在模块路径中,因此您必须先添加它:
require.paths.push('.')
var test = require("test.class");