Java Execution JS Script

Source: Internet
Author: User

Aaa

package cn.sniper.spider.utils;import java.io.bufferedreader;import java.io.fileinputstream; Import java.io.inputstream;import java.io.inputstreamreader;import java.io.stringreader;import  java.net.URL;import javax.script.Invocable;import javax.script.ScriptEngine;import  javax.script.scriptenginemanager;import org.junit.test;public class java4jsutil {          @Test     public void testjs1 ()   throws exception {        scriptenginemanager  Scriptenginemanager = new scriptenginemanager ();         scriptengine engine = scriptenginemanager.getenginebyextension ("JS");                 stringreader reader =  new stringreader ("function  Getnum (num) {return num} ");                 engine.eval (reader);                 Invocable invocable =  (invocable) engine;         object result = invocable.invokefunction ("Getnum",  "2");         SYSTEM.OUT.PRINTLN (Result);    }          @Test     public void testjs2 ()  throws  Exception {        scriptenginemanager scriptenginemanager  = new scriptenginemanager ();        scriptengine  Engine = scriptenginemanager.getenginebyextension ("JS");         bufferedreader  Reader = new bufferedreader (New inputstreamreader (New fileinputstream ("D:\\test.js") ));                 engine.eval (reader);                 invocable invocable =  (invocable) engine;        object  result = invocable.invokefunction ("Getnum",  "2");         SYSTEM.OUT.PRINTLN (Result);    }          @Test     public void testonlinejs ()  throws exception {         scriptenginemanager scriptenginemanager = new  scriptenginemanager ();         scriptengine engine =  scriptenginemanager.getenginEbyextension ("JS");         url url2 = new url (" Http://10.161.56.231:8888/tjfxpt/resources/js/common/common.js ");         inputstream inputstream = url2.openstream ();         bufferedreader reader = new bufferedreader (New inputstreamreader (InputStream));         system.out.println (Reader.readline ());         engine.eval (reader);                 Invocable invocable =  (invocable) engine;         object result = invocable.invokefunction ("Fmoney", new  INTEGER[]{10, 2});         system.out.println (Result);             }    } 


Java Execution JS Script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.