Java程序中实现调用Python脚本的方法详解
作者:EliteQing 发布时间:2021-07-08 22:00:08
本文实例讲述了Java程序中实现调用Python脚本的方法。分享给大家供大家参考,具体如下:
在程序开发中,有时候需要Java程序中调用相关Python脚本,以下内容记录了先关步骤和可能出现问题的解决办法。
1、在Eclipse中新建Maven工程;
2、pom.xml文件中添加如下依赖包之后update maven工程;
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
3、编写如下测试代码;
import org.python.util.PythonInterpreter;
public class JpythonScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days[1];");
}
}
4、测试:
出现如下错误:
console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
* sys.path: ['...python\\jython\\2.7.0\\Lib', '__classpath__', '__pyclasspath__/']
This attribute might be including the wrong directories, such as from CPython
* sys.prefix:***\jython\2.7.0
This attribute is set by the system property python.home, although it can
be often automatically determined by the location of the Jython jar file
You can use the -S option or python.import.site=false to not import the site module
5、问题解决:
重构代码如下:
import java.util.Properties;
import org.python.util.PythonInterpreter;
public class JpythonScript {
public static void main(String args[]) {
Properties props = new Properties();
props.put("python.home", "path to the Lib folder");
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days[1];");
}
}
6、编译成功。
7、解决问题参考:
http://bugs.jython.org/issue2355
补充:jpython抛错Cannot import site module的解决方法
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
public class JpythonScript {
public static void main(String args[]) {
Properties props = new Properties();
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days[1];");
}
// 进行复杂类接受处理
Map<String, Object> res = new HashMap<String, Object>();
res.put("1", "Danny");
res.put("2", "Fanny");
PythonInterpreter interpM = new PythonInterpreter();
interpM.execfile("./src/com/DataDeal.py");
PyFunction pyFunctionM = (PyFunction) interpM.get("main", PyFunction.class);
Map<PyObject, PyObject> tableM = new HashMap<PyObject, PyObject>();
tableM.put(new PyString("conf"), PyJavaType.wrapJavaObject(res));
PyDictionary pydM = new PyDictionary(tableM);
更多java相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
来源:http://www.cnblogs.com/liinux/p/5481849.html


猜你喜欢
- 本文章是基于鸿洋的Android 自定义View (一) 的一些扩展,以及对Android自定义View构造函数详解里面内容的一些转载。首先
- 如何打印GC日志排查问题在工作当中,有时候我们会需要打印GC的相关信息来定位问题。该如何做呢?先来看个示例public static voi
- 前言多数据源的事务处理是个老生常谈的话题,跨两个数据源的事务管理也算是分布式事务的范畴,在同一个JVM里处理多数据源的事务,比较经典的处理方
- 本文以实例形式讲述了C#解析JSON的方法,C#封装了对XML和JSON解析的类库,使用相当方便!具体用法如下:1.主要用到的类:主要用到了
- 现在的模拟器的功能太强大,从蓝牙,传感器等配件到IMEI,Mac,以及手机硬件信息什么都可以模拟为了防止用户利用模拟器模仿真机进行刷单,刷流
- What内含一个或多个maven模块的SpringBoot项目称为SpringBoot多模块项目Why便于维护 将一个大的单体项目分成多个子
- 问题提出:一般在生产环境上,日志的级别是INFO以上,但有时候程序出现问题(如SQL报错),少量日志不能尽快定位问题,这时候可以动态修改日志
- 一、简介应用程序配置文件(App.config)是标准的 XML 文件,XML 标记和属性是区分大小写的。它是可以按需要更改的,开发人员可以
- Struct的理论看过好一些,可是工作上基本没有应用过,Class倒处处都有。难道Struct就没有什么使用价值吗?搜了一下如何在类和结构中
- 计算两点之间的距离然后在控制台输出,这个题目还是挺简单的。下面我们来看看具体代码。package com.swift;import java
- 在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户
- 在C#程序开发过程中,很多时候可能需要将字符串根据特定的分割字符分割成字符或者List集合,例如根据逗号将字符串分割为数组,或者根据竖线将字
- SpringBoot Admin 实现Actuator端点可视化监控简介Actuator可视化监控SpringBoot AdminNote:
- ES 简介Elasticsearch 是一个基于 Lucene 实现的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 REST
- 本文实例讲述了退出Android程序时清除所有activity的方法。分享给大家供大家参考,具体如下:在一个项目中,要退出android程序
- Java8中的lambda表达式、::符号和Optional类 0. 函数式编程 函
- 本文实例讲述了Android中WebView用法。分享给大家供大家参考,具体如下:WebView相当于一个迷你浏览器,采用WebKit内核,
- 一、TestNG介绍TestNG是Java中的一个测试框架, 类似于JUnit 和NUnit, 功能都差不多, 只是功能更加强大,使用也更方
- 本文实例为大家分享了Java实现通讯录管理系统的具体代码,供大家参考,具体内容如下一、前言我们学了这么久的知识了,光学知识不会用是一件很悲伤
- 先看看效果图:分析: 根据敌机类型区分 敌机 运动逻辑 以及绘制/** * 敌机 * * @author liuml * @time 20