当前访客身份:游客 [ 登录  | 注册加入尚学堂]
直播

安全缔造者

拥有积分:0
Study until I'm dead!

博客分类

笔记中心

课题中心

提问中心

答题中心

解答题中心

ReferenceError: "importPackage" is not defined in <eval> at line number 1   悬赏积分3

安全缔造者 提问于 7月前 (2016-07-02 09:19:40)  | 回答(0 )  

//用“myeclipse-2016-ci-3-offline-intaller-windows”编译的,出现了上面的问题,用尚学堂源代码还是同样的问题???

package xxx.test;

import java.util.List;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Demo01 {

 public static void main(String[] args) throws Exception {
  
  ScriptEngineManager sem = new ScriptEngineManager();
  ScriptEngine engine = sem.getEngineByName("javascript");
  
  engine.put("msg", "xxx is a good man!");
  String str = "var user = {name:'xxx',age:30,schools:['xxxxx','xxxxxxx']};";
  str += "print\n(user.name);";
  engine.eval(str);
  engine.eval("msg = 'xxxxxxx is a good corporation!';");
  
  
  System.out.println(engine.get("msg"));
  System.out.println("######################################");
  
  engine.eval("function add(a,b){var sum = a+b;return sum;}");
  
  Invocable jsInvoke = (Invocable) engine;
  Object result1 = jsInvoke.invokeFunction("add", new Object[]{13,20});
  System.out.println(result1);
  
  String jsCode = "importPackage(java.util); var list = Arrays.asList([\"xxxxxxxxxxxx\",\"xxxxxxx\",\"xxxxxxxxxx\"]);";
  engine.eval(jsCode);
  List<String> list2 = (List<String>) engine.get("list");
  for (String temp : list2) {
   System.out.println(temp);
  }
 }
}package sgqg.liuhonglei.study.test;

import java.util.List;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Demo01 {

 public static void main(String[] args) throws Exception {
  
  ScriptEngineManager sem = new ScriptEngineManager();
  ScriptEngine engine = sem.getEngineByName("javascript");
  
  engine.put("msg", "liuhonglei is a good man!");
  String str = "var user = {name:'liuhonglei',age:30,schools:['dongbeidaxue','shouganggufengongsi']};";
  str += "print\n(user.name);";
  engine.eval(str);
  engine.eval("msg = 'shouganggufengongsi is a bad corporation!';");
  
  
  System.out.println(engine.get("msg"));
  System.out.println("######################################");
  
  engine.eval("function add(a,b){var sum = a+b;return sum;}");
  
  Invocable jsInvoke = (Invocable) engine;
  Object result1 = jsInvoke.invokeFunction("add", new Object[]{13,20});
  System.out.println(result1);
  
  String jsCode = "importPackage(java.util); var list = Arrays.asList([\"首钢股份有限公司\",\"硅钢事业部\",\"三作业区\"]);";
  engine.eval(jsCode);
  List<String> list2 = (List<String>) engine.get("list");
  for (String temp : list2) {
   System.out.println(temp);
  }
  
  
  
  
 }
}myeclipse-2016-ci-3-offline-installer-windows

回复提问(选为最优答案得到3积分)

北京总部地址:北京市海淀区西三旗桥东建材城西路85号神州科技园B座三层尚学堂 咨询电话:400-009-1906 010-56233821
Copyright 2007-2015 北京尚学堂科技有限公司 京ICP备13018289号-1 京公网安备11010802015183