在Rhino中获取脚本的路径

发布于 2024-11-08 07:06:05 字数 331 浏览 4 评论 0原文

我正在尝试获取在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有一腔孤勇 2024-11-15 07:06:06

不可能做你想做的事。

检测 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).

深者入戏 2024-11-15 07:06:06
/**
 * Gets the name of the running JavaScript file.
 *
 * REQUIREMENTS:
 * 1. On the Java command line, for the argument that specifies the script's
 *    name, there can be no spaces in it. There can be spaces in other 
 *    arguments, but not the one that specifies the path to the JavaScript 
 *    file. Quotes around the JavaScript file name are irrelevant. This is
 *    a consequence of how the arguments appear in the sun.java.command
 *    system property.
 * 2. The following system property is available: sun.java.command
 *
 * @return {String} The name of the currently running script as it appeared 
 *                  on the command line.
 */
function getScriptName() {
    var scriptName = null;

    // Put all the script arguments into a string like they are in 
    // environment["sun.java.command"].
    var scriptArgs = "";
    for (var i = 0; i < this.arguments.length; i++) {
        scriptArgs = scriptArgs + " " + this.arguments[i];
    }

    // Find the script name inside the Java command line.
    var pattern = " (\\S+)" + scriptArgs + "$";
    var scriptNameRegex = new RegExp(pattern);
    var matches = scriptNameRegex.exec(environment["sun.java.command"]);
    if (matches != null) {
        scriptName = matches[1];
    }
    return scriptName;
}

/**
 * Gets a java.io.File object representing the currently running script. Refer
 * to the REQUIREMENTS for getScriptName().
 *
 * @return {java.io.File} The currently running script file
 */
function getScriptFile() {
    return new java.io.File(getScriptName());
}

/**
 * Gets the absolute path name of the running JavaScript file. Refer to
 * REQUIREMENTS in getScriptName().
 *
 * @return {String} The full path name of the currently running script
 */
function getScriptAbsolutePath() {
    return getScriptFile().getAbsolutePath();
}
/**
 * Gets the name of the running JavaScript file.
 *
 * REQUIREMENTS:
 * 1. On the Java command line, for the argument that specifies the script's
 *    name, there can be no spaces in it. There can be spaces in other 
 *    arguments, but not the one that specifies the path to the JavaScript 
 *    file. Quotes around the JavaScript file name are irrelevant. This is
 *    a consequence of how the arguments appear in the sun.java.command
 *    system property.
 * 2. The following system property is available: sun.java.command
 *
 * @return {String} The name of the currently running script as it appeared 
 *                  on the command line.
 */
function getScriptName() {
    var scriptName = null;

    // Put all the script arguments into a string like they are in 
    // environment["sun.java.command"].
    var scriptArgs = "";
    for (var i = 0; i < this.arguments.length; i++) {
        scriptArgs = scriptArgs + " " + this.arguments[i];
    }

    // Find the script name inside the Java command line.
    var pattern = " (\\S+)" + scriptArgs + "$";
    var scriptNameRegex = new RegExp(pattern);
    var matches = scriptNameRegex.exec(environment["sun.java.command"]);
    if (matches != null) {
        scriptName = matches[1];
    }
    return scriptName;
}

/**
 * Gets a java.io.File object representing the currently running script. Refer
 * to the REQUIREMENTS for getScriptName().
 *
 * @return {java.io.File} The currently running script file
 */
function getScriptFile() {
    return new java.io.File(getScriptName());
}

/**
 * Gets the absolute path name of the running JavaScript file. Refer to
 * REQUIREMENTS in getScriptName().
 *
 * @return {String} The full path name of the currently running script
 */
function getScriptAbsolutePath() {
    return getScriptFile().getAbsolutePath();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文