【问题标题】:Getting the path of a script in Rhino在 Rhino 中获取脚本的路径
【发布时间】:2011-05-18 18:15:43
【问题描述】:

我正在尝试获取在 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 作为参数传递,但这不是我正在寻找的解决方案。

【问题讨论】:

    标签: java javascript rhino


    【解决方案1】:

    不可能做你想做的。

    检测由 JavaScript 解释器运行的脚本源的能力不是 ECMAScript 语言规范或 Rhino shell extensions 的一部分。

    但是,您可以编写一个封装可执行程序,它以脚本路径作为参数并在 Rhino 中执行脚本(例如,通过调用适当的主类)并将脚本位置作为环境变量(或类似变量)提供。

    【讨论】:

    • 谢谢,我害怕这个。我从小道消息中听说,Rhino 开发人员认为包含这样的内容没有任何意义,并且没有计划发布任何版本。不幸的是,Rhino 需要它,因为他们对 require 的实现不完整。 Node.js 确实提供了它,所以我认为 Rhino 也可以。您的建议正是目前的实施方式。
    【解决方案2】:
    /**
     * 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();
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-10
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 2011-08-16
      相关资源
      最近更新 更多