在Rhino中获取脚本的路径
我正在尝试获取在 Rhino 中执行的脚本的路径。我希望不必将目录作为第一个参数传递。我什至不知道如何获得它。我目前正在通过调用 Rhino
java -jar /some/path/to/js.jar -modules org.mozilla.javascript.commonjs.module /path/to/myscript.js
,并希望 myscript.js 能够识别 /path/to 因为它是目录名,无论我从哪里运行此脚本。唯一的其他相关问题& StackOverflow 上的建议是将 /path/to 作为参数传递,但这不是我正在寻找的解决方案。
I'm trying to get the path to a script executing in Rhino. I would prefer to not have to pass in the directory as the first argument. I don't even have a lead on how to get it. I'm currently calling Rhino via
java -jar /some/path/to/js.jar -modules org.mozilla.javascript.commonjs.module /path/to/myscript.js
and would like myscript.js to recognize /path/to as it's dirname, regardless of where I run this script from. The only other related question & suggestion here on StackOverflow is to pass /path/to as an argument, but that is not the solution I am looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可能做你想做的事。
检测 JavaScript 解释器运行的脚本源的能力不属于 ECMAScript 语言规范或 Rhino shell 扩展。
然而,您可以编写一个包装器可执行程序,它采用脚本路径作为其参数,并在Rhino中执行脚本(例如通过调用适当的主类),并提供脚本位置作为环境变量(或类似变量)。
It's not possible to do what you want.
The ability to detect the source of the script being run by a JavaScript interpreter is not a part of the ECMAScript language specification or the Rhino shell extensions.
However, you could write a wrapper executable program which takes a script path as its argument and executes the script in Rhino (e.g. by calling the appropriate main class) and also providing the script location as an environment variable (or similar).