java programming to implement the call js method analysis

  • 2020-09-16 07:30:11
  • OfStack

This article illustrates the implementation of calling the js method in java programming. To share for your reference, the details are as follows:


/*
*  Load the script engine and in java In the call js methods 
*/
public void test2()
{
     ScriptEngineManager manager = new ScriptEngineManager();
     ScriptEngine engine = manager.getEngineByName("javascript");
     try {
       String str="2&1";
       Double d = (Double) engine.eval(str);
       Integer i=d.intValue();
       System.out.println(i);
     } catch (ScriptException ex) {
      ex.printStackTrace();
     }
}


/*
*  in java In the call js . jdk1.6 In the loading js Engine class, which is then called by it js Methods. 
*  And through the JDK Platform to script Parameter assignment in the 
*/
public void test(String name)
{
    ScriptEngineManager sem = new ScriptEngineManager();
    /*
     *sem.getEngineByExtension(String extension) Parameters for js
     sem.getEngineByMimeType(String mimeType)  Parameters for application/javascript  or text/javascript
     sem.getEngineByName(String shortName) Parameters for js or javascript or JavaScript
     */
    ScriptEngine se = sem.getEngineByName("js");
    try
    {
     String script = "function say(){ return 'hello,'"+name+"; }";
     se.eval(script);
     Invocable inv2 = (Invocable) se;
     String res=(String)inv2.invokeFunction("say",name);
     System.out.println(res);
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
}


/*
*  Show how to put java Medium object as js Global variables, while the demonstration will file Class is assigned to the scripting language and gets its properties. 
*/
public void testScriptVariables()
{
    ScriptEngineManager sem=new ScriptEngineManager();
    ScriptEngine engine=sem.getEngineByName("js");
    File file=new File("c://1.txt");
    engine.put("f", file);
    try {
      engine.eval("println('path:'+f.getPath())");// Can't use alert methods 
    } catch (ScriptException e) {
      e.printStackTrace();
    }
}


/*
*  Show how to use java How to start by thread 1 a js methods 
*/
public void testScriptInterface() throws ScriptException
{
    ScriptEngineManager sem=new ScriptEngineManager();
    ScriptEngine engine=sem.getEngineByName("js");
    String script="var obj=new Object();obj.run=function(){println('test thread')}";
    engine.eval(script);
    Object obj=engine.get("obj");// To obtain js Objects in the 
    Invocable inv=(Invocable)engine;
    Runnable r=inv.getInterface(obj,Runnable.class);
    Thread t=new Thread(r);
    t.start();
}

For more information about java algorithm, please refer to Java character and string manipulation skills summary, Java array manipulation skills Summary, Java Mathematical Manipulation Skills Summary, Java Encoding manipulation Skills Summary and Java Data Structure and Algorithm Tutorial

I hope this article has been helpful in java programming.


Related articles: