2013-10-27 50 views
2

我真的搞清楚腳本Java的支持,我現在有這個測試類的Java ScriptingManager訪問和使用功能

import java.io.FileNotFoundException; 
import javax.script.Invocable; 
import javax.script.ScriptEngine; 
import javax.script.ScriptEngineManager; 
import javax.script.ScriptException; 



/** 
* 
* @author Makerimages 
*/ 
public class main { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) throws ScriptException, FileNotFoundException, NoSuchMethodException { 

    // create a script engine manager 
    ScriptEngineManager factory = new ScriptEngineManager(); 
    // create JavaScript engine 
    ScriptEngine engine = factory.getEngineByName("JavaScript"); 
    // evaluate JavaScript code from given file - specified by first argument 
    engine.eval(new java.io.FileReader("C:/Users/**/Documents/NetBeansProjects/test/src/l.js")); 

} 
public void makeObject(String s) 
{ 
    System.out.println(s); 
} 

} 

這l.js文件

makeObject("afzsdf"); 

當我嘗試運行這個我得到

run: Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "makeObject" is not defined. (#1) in at line number 1 at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:224) at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249) at main.main(main.java:34) Caused by: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "makeObject" is not defined. (#1) at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3771) at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3749) at sun.org.mozilla.javascript.internal.ScriptRuntime.notFoundError(ScriptRuntime.java:3834) at sun.org.mozilla.javascript.internal.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2219) at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1510) at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:849) at sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:162) at sun.org.mozilla.javascript.internal.ContextFactory.doTopCall(ContextFactory.java:429) at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:116) at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:109) at sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:3161) at sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:173) at sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1159) at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:214) ... 2 more Java Result: 1 BUILD SUCCESSFUL (total time: 3 seconds)

如何解決它,所以我可以在腳本中使用makeObject函數?

+0

你閱讀文件? http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/ – tom

回答

0

您需要添加綁定,例如將你的Java對象鏈接到JS範圍。

的Java:

import javax.script.*; 
import java.io.FileNotFoundException; 

public class main { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws ScriptException, FileNotFoundException, NoSuchMethodException { 

     // create a script engine manager 
     ScriptEngineManager factory = new ScriptEngineManager(); 
     // create JavaScript engine 
     ScriptEngine engine = factory.getEngineByName("JavaScript"); 

     //bind main instance 
     Bindings bindings = engine.createBindings(); 
     bindings.put("main", new main()); 
     engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE); 

     // evaluate JavaScript code from given file - specified by first argument 
     engine.eval(new java.io.FileReader("l.js")); 

    } 

    public void makeObject(String s) { 
     System.out.println(s); 
    } 
} 

在JS你引用此main對象:

main.makeObject("afzsdf"); 
+0

Brialliant!創造奇蹟!但是,是否還有一種方法可以評估整個文件夾的價值.js文件以及該文件夾的子文件夾? – Makerimages

+0

不客氣!我不認爲有這樣的方法 - 你只需以正確的順序迭代文件並編譯它們。如果您遇到麻煩,請隨時向我提問。 –