Java程序中实现调用Python脚本的方法详解

 更新时间:2018年03月29日 10:55:04   作者:EliteQing  
这篇文章主要介绍了Java程序中实现调用Python脚本的方法,结合实例形式分析了eclipse环境中使用Java调用Python脚本的相关操作技巧与注意事项,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • SpringBoot启动后启动内嵌浏览器的方法

    SpringBoot启动后启动内嵌浏览器的方法

    这篇文章主要介绍了SpringBoot启动后启动内嵌浏览器的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • selenium-java实现自动登录跳转页面方式

    selenium-java实现自动登录跳转页面方式

    利用Selenium和Java语言可以编写一个脚本自动刷新网页,首先,需要确保Google浏览器和Chrome-Driver驱动的版本一致,通过指定网站下载对应版本的浏览器和驱动,在Maven项目中添加依赖,编写脚本实现网页的自动刷新,此方法适用于需要频繁刷新网页的场景,简化了操作,提高了效率
    2024-11-11
  • SpringBoot浅析安全管理之Shiro框架

    SpringBoot浅析安全管理之Shiro框架

    安全管理是软件系统必不可少的的功能。根据经典的“墨菲定律”——凡是可能,总会发生。如果系统存在安全隐患,最终必然会出现问题,这篇文章主要介绍了SpringBoot安全管理Shiro框架的使用
    2022-08-08
  • java爬取豆瓣电影示例解析

    java爬取豆瓣电影示例解析

    这篇文章主要介绍了java爬取豆瓣电影示例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • 详解Java中Period类的使用方法

    详解Java中Period类的使用方法

    Period类通过年、月、日相结合来描述一个时间量,最高精度是天。本文将通过示例详细为大家讲讲Period类的使用,需要的可以参考一下
    2022-05-05
  • Java线程在什么情况下可以终止

    Java线程在什么情况下可以终止

    Thread线程类自带的stop方法,但是jdk不建议使用,因为stop方法终止线程只是强行终止,内存中部分值可能已发生变化,并未保证数据的一致性,将会导致线程安全问题,那么在什么情况下可以终止线程呢,本篇带你探究一下
    2022-04-04
  • 快速掌握Java中注解与反射

    快速掌握Java中注解与反射

    本文详细介绍了Java中注解和反射的概念及应用,注解是用于给代码添加元数据的标记,如@Override、@Deprecated等,反射机制则是在运行时获取和操作类的内部信息,提高了代码的灵活度,感兴趣的朋友跟随小编一起看看吧
    2023-06-06
  • 5个并发处理技巧代码示例

    5个并发处理技巧代码示例

    这篇文章主要介绍了5个并发处理技巧代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-10-10
  • Java全排列算法字典序下的下一个排列讲解

    Java全排列算法字典序下的下一个排列讲解

    今天小编就为大家分享一篇关于Java全排列字典序下的下一个排列,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • java读写串口数据你了解多少

    java读写串口数据你了解多少

    这篇文章主要为大家详细介绍了java读写串口数据,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02

最新评论