替换<未知来源>在 Java Rhino (JSR223) 中使用实际文件名未知来源>
在我的代码中,所有脚本都包含在 .js 文件中。每当其中一个脚本包含错误时,我都会收到以下消息:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "nonexistant" is not Defined。 (<未知来源>#5)在<未知来源>中第 5 行
让我烦恼的是<未知来源>。一个 ScriptContext 中有多个文件,因此很难追踪到错误。它看起来也很可怕。
有没有办法替换<未知来源>与实际的文件名?我看到的方法都不支持传递 File 对象,所以我在这里真的很困惑。
In my code, all of the scripts are contained in .js files. Whenever one of the scripts contains an error, I get this:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "nonexistant" is not defined. (<Unknown source>#5) in <Unknown source> at line number 5
What bugs me is the <Unknown Source>. Multiple files are in one ScriptContext, and it can be hard to track down an error. It also looks horrible.
Is there a way to replace <Unknown Source> with the actual file name? None of the methods I see support passing a File object, so I'm really confused here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 ScriptEngine.FILENAME 常量:
scriptEngine.put(ScriptEngine.FILENAME, scriptFile.toString());
Use the ScriptEngine.FILENAME constant:
scriptEngine.put(ScriptEngine.FILENAME, scriptFile.toString());
这个问题还没有被专门提出,但我想我会向将来偶然发现这个主题的任何人提供这个:当 Java 8 发布并且我们从 Rhino 转向 Nashorn 作为底层 JavaScript 引擎时,这将会改变。在 Nashorn 下,文件名应用于 ScriptContext,而不是 ScriptEngine 本身:
如果您尝试使用 ScriptEngine.put() 应用文件名,就像您在 Rhino 下所做的那样,则不会发生任何事情,并且您的异常将返回“< ;评估>”作为文件名。
我想在接下来的几个月里会有一些人遇到这个问题,所以我想我会提供它。这似乎没有任何地方记录。我必须深入研究 Nashorn 源代码才能弄清楚。
The question hasn't been specifically asked yet, but I thought I'd offer this to anyone who stumbles upon this topic in the future: this will change when Java 8 is released and we move from Rhino to Nashorn as the underlying JavaScript engine. Under Nashorn, the file name is applied to the ScriptContext, rather than to the ScriptEngine itself:
If you attempt to apply the file name using ScriptEngine.put(), as you do under Rhino, nothing will happen and your exceptions will return "<eval>" as the file name.
I would imagine that a few people will run into this issue in the coming months, so thought I'd offer it. This does not appear to be documented anywhere. I had to dig into the Nashorn source code to figure it out.
通过 mattj65816 提出的 ScriptContext 设置脚本引擎文件名的 Java 8 (Nashorn) 方法也适用于 Rhino 引擎。因此,我建议仅使用,
因为这段代码适用于两种常见的 JavaScript 引擎。您不需要创建自己的上下文,而只需将属性设置为引擎的默认上下文:
The Java 8 (Nashorn) way of setting the filename for the script engine through the ScriptContext figured out by mattj65816, works for the Rhino engine as well. So, I'd recommend using only
since this piece of code works for both common JavaScript engines. You don't event need to create you own context, but only set the attribute to the engine's default context:
完美的!
输出 :
perfect!
output :