您的位置:首页 > 编程语言 > Python开发

Python之玩转Jython系列(一)

2015-04-09 16:06 483 查看
,java 调用jython

简单调用:

PythonInterpreter interp1 = new PythonInterpreter();
		interp1.exec("import re");
		interp1.execfile("./src/com/configValue.py");

		PyFunction pyFunction1 = (PyFunction) interp1.get("jython里的方法名",
				PyFunction.class);
		System.out.println("config value: "
				+ pyFunction1.__call__(new PyString("MaxValue")));


但在项目中,这样会带来很多麻烦,特此修改了下,如下:

PythonInterpreter interp = new PythonInterpreter();
		Map<PyObject, PyObject> bean = new HashMap<PyObject, PyObject>();
		 //取得根目录路径  
	        String rootPath=getClass().getResource("../").getFile().toString();  
		String path =  rootPath + "xxx/xxx.py"; 
		interp.execfile(path);
		PyFunction func = interp.get("jython里的方法名", PyFunction.class);
		bean.put(new PyString("key"),
				PyJavaType.wrapJavaObject(jythonMethod));
		PyDictionary pyDictionary = new PyDictionary(bean);
		obj = func.__call__(pyDictionary);
		return obj;
这样的话,就可以调用到对应的jython了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: