खोज…


परिचय

जावा अपने आप में एक बहुत शक्तिशाली भाषा है, लेकिन इसकी शक्ति को आगे बढ़ाया जा सकता है। JSR223 के लिए धन्यवाद (जावा विशिष्टता अनुरोध 223) एक स्क्रिप्ट इंजन की शुरुआत

टिप्पणियों

जावा स्क्रिप्टिंग एपीआई बाहरी स्क्रिप्ट को जावा के साथ बातचीत करने में सक्षम बनाता है

स्क्रिप्टिंग एपीआई स्क्रिप्ट और जावा के बीच बातचीत को सक्षम कर सकता है। स्क्रिप्टिंग भाषाओं में क्लासपैथ पर स्क्रिप्ट इंजन का कार्यान्वयन होना चाहिए।

डिफ़ॉल्ट रूप से जावास्क्रिप्ट (ईसीएमएस्क्रिप्ट के रूप में भी जाना जाता है) डिफ़ॉल्ट रूप से nashorn द्वारा प्रदान किया जाता है। प्रत्येक स्क्रिप्ट इंजन में एक स्क्रिप्ट संदर्भ होता है जहां सभी चर, कार्य, विधियां बाइंडिंग में संग्रहीत की जाती हैं। कभी-कभी आप कई संदर्भों का उपयोग करना चाह सकते हैं क्योंकि वे आउटपुट को बफ़र किए गए राइटर और दूसरे को त्रुटि पर पुनर्निर्देशित करने का समर्थन करते हैं।

कई अन्य स्क्रिप्ट इंजन लाइब्रेरी हैं जैसे कि Jython और JRuby। जब तक वे क्लासपाथ पर हैं तब तक आप कोड निकाल सकते हैं।

हम स्क्रिप्ट में वेरिएबल्स को उजागर करने के लिए बाइंडिंग का उपयोग कर सकते हैं। हमें कुछ मामलों में कई बाइंडिंग की आवश्यकता होती है क्योंकि इंजन को वैरिएबल एक्सपोज करने के लिए मूल रूप से केवल उस इंजन के लिए वैरिएबल को एक्सपोज करना होता है, कभी-कभी हमें सिस्टम वैरिएबल और पथ जैसे कुछ वैरिएबल को एक्सपोज करने की आवश्यकता होती है जो एक ही प्रकार के सभी इंजनों के लिए समान हो। उस मामले में, हमें एक बंधन की आवश्यकता है जो एक वैश्विक गुंजाइश है। उसी के लिए चर का एक्सपोजर इसे उसी EngineFactory द्वारा बनाए गए सभी स्क्रिप्ट इंजनों को उजागर करता है

एक जावास्क्रिप्ट फ़ाइल का मूल्यांकन nashorn की -scripting मोड में

public class JSEngine {
    
    /*
    * Note Nashorn is only available for Java-8 onwards
    * You can use rhino from ScriptEngineManager.getEngineByName("js");
    */
    
    ScriptEngine engine;
    ScriptContext context;
    public Bindings scope;
    
    // Initialize the Engine from its factory in scripting mode
    public JSEngine(){
        engine = new NashornScriptEngineFactory().getScriptEngine("-scripting");
        // Script context is an interface so we need an implementation of it
        context = new SimpleScriptContext();
        // Create bindings to expose variables into
        scope = engine.createBindings();
    }
    
    // Clear the bindings to remove the previous variables
    public void newBatch(){
        scope.clear();
    }
    
    public void execute(String file){
        try {
            // Get a buffered reader for input
            BufferedReader br = new BufferedReader(new FileReader(file));
            // Evaluate code, with input as bufferdReader
            engine.eval(br);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(JSEngine.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ScriptException ex) {
            // Script Exception is basically when there is an error in script
            Logger.getLogger(JSEngine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void eval(String code){
        try {
            // Engine.eval basically treats any string as a line of code and evaluates it, executes it
            engine.eval(code);
        } catch (ScriptException ex) {
            // Script Exception is basically when there is an error in script
            Logger.getLogger(JSEngine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    // Apply the bindings to the context and set the engine's default context
    public void startBatch(int SCP){
        context.setBindings(scope, SCP);
        engine.setContext(context);
    }
    
    // We use the invocable interface to access methods from the script
    // Invocable is an optional interface, please check if your engine implements it
    public Invocable invocable(){
        return (Invocable)engine;
    }
    
}

अब मुख्य विधि है

public static void main(String[] args) {
    JSEngine jse = new JSEngine();
    // Create a new batch probably unecessary
    jse.newBatch();
    // Expose variable x into script with value of hello world
    jse.scope.put("x", "hello world");
    // Apply the bindings and start the batch
    jse.startBatch(ScriptContext.ENGINE_SCOPE);
    // Evaluate the code
    jse.eval("print(x);");
}

आपका आउटपुट इसके समान होना चाहिए
hello world

जैसा कि आप देख सकते हैं कि एक्सपैक्टेड वेरिएबल x प्रिंट हो गया है। अब एक फ़ाइल के साथ परीक्षण।

यहां हमने परीक्षण किया है। जे.एस.

print(x);
function test(){
    print("hello test.js:test");
}
test();

और अद्यतन मुख्य विधि

public static void main(String[] args) {
    JSEngine jse = new JSEngine();
    // Create a new batch probably unecessary
    jse.newBatch();
    // Expose variable x into script with value of hello world
    jse.scope.put("x", "hello world");
    // Apply the bindings and start the batch
    jse.startBatch(ScriptContext.ENGINE_SCOPE);
    // Evaluate the code
    jse.execute("./test.js");
}

मान लें कि test.js आपके अनुप्रयोग के समान निर्देशिका में है, आपके पास इसके समान आउटपुट होना चाहिए

hello world
hello test.js:test


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow