相当于Ruby';s";要求;在Javascript中

相当于Ruby';s";要求;在Javascript中,javascript,rhino,Javascript,Rhino,如何在Javascript中“要求”另一个文件进入现有文件?有什么类似于Ruby的“require”或“load”吗 >注意:我在服务器(Rhino)中使用JS 原因:我只需要访问其他JS文件中的方法 更新:这仅在从cmd行执行时有效。当我试图用程序调用它时,它失败了。以下是我的代码:在Rhino shell中,您应该能够使用,这是一种预定义的全局方法: 加载([filename,…]) 加载由字符串参数命名的JavaScript源文件。如果给定多个参数,则依次读取和执行每个文件 要在Java嵌

如何在Javascript中“要求”另一个文件进入现有文件?有什么类似于Ruby的“require”或“load”吗

>注意:我在服务器(Rhino)中使用JS

原因:我只需要访问其他JS文件中的方法


更新:这仅在从cmd行执行时有效。当我试图用程序调用它时,它失败了。以下是我的代码:

在Rhino shell中,您应该能够使用,这是一种预定义的全局方法:

加载([filename,…])


加载由字符串参数命名的JavaScript源文件。如果给定多个参数,则依次读取和执行每个文件


要在Java嵌入的js中使用load函数,必须首先在脚本上下文中公开它。可能有一种方法可以从Java实现,但也可以使用js实现

免责声明:此解决方案使用的源代码取自我一直从事的Apache许可项目。您可以看到原始源文件

此js文件设置全局变量,并位于名为setupglobals.js的文件中:

var shell = org.mozilla.javascript.tools.shell.Main;
var args = ["-e","var a='STRING';"];
shell.exec(args);

var shellGlobal = shell.global;

//grab functions from shell global and place in current global
load=shellGlobal.load;
print=shellGlobal.print;
defineClass=shellGlobal.defineClass;
deserialize=shellGlobal.deserialize;
doctest=shellGlobal.doctest;
gc=shellGlobal.gc;
help=shellGlobal.help;
loadClass=shellGlobal.loadClass;
quit=shellGlobal.quit;
readFile=shellGlobal.readFile;
readUrl=shellGlobal.readUrl;
runCommand=shellGlobal.runCommand;
seal=shellGlobal.seal;
serialize=shellGlobal.serialize;
spawn=shellGlobal.spawn;
sync=shellGlobal.sync;
toint32=shellGlobal.toint32;
version=shellGlobal.version;
environment=shellGlobal.environment;
这是您的原始Java主机文件,现在已扩展为在任何其他脚本之前评估setupglobals.js:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.mozilla.javascript.*;

public class RhinoRunner {
    public static void main(String args[]) throws IOException 
    {
        BufferedReader script = new BufferedReader(new FileReader("setupglobals.js"));
        BufferedReader script2 = new BufferedReader(new FileReader("example.js"));
        Context context = Context.enter();
        try {
            ScriptableObject scope = context.initStandardObjects();
            context.evaluateReader(scope, script, "script", 1, null);
            context.evaluateReader(scope, script2, "script2", 1, null);
            Function fct = (Function)scope.get("abc", scope);
            Object result = fct.call(context, scope, scope, new Object[] {2, 3});
            System.out.println(Context.jsToJava(result, int.class));
        } 
        finally 
        {
            Context.exit();
        }
    }
}
下面是您的example.js,现在已扩展为使用全局加载函数加载文件hello.js:

function abc(x,y) 
{
    return x+y 
}

load("hello.js")
print("hello world!")
最后,这里是hello.js:

function abc(x,y) 
{
    return x+y 
}

load("hello.js")
print("hello world!")
执行时,RhinoRunner将打印以下内容:

hello world!
5

不,这对我不起作用。我有个错误。下面是代码+错误:@instantsetsuna:它对我有用。。。其他预定义函数是否工作?像
print()
?不,print()也不起作用。我使用的是mozilla rhino 1.7r2,而不是JDK附带的版本。也许是因为这个?这很奇怪。上游rhino明确地将load()和print()捆绑为其默认环境的一部分。你是如何执行的?“java-jar js.jar”?@Echo:我已经更新了我的问题。它可以在cmd行中正常工作,但当嵌入java.Wow中时会失败!它工作得很好!非常感谢!:)如果您不介意的话,您能解释一下setupglobals.js的前几行中发生了什么吗?它设置并执行Rhino shell。这有点神奇,但它很管用。