在程序开发中,有时候需要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、编写如下测试代码;

1 import org.python.util.PythonInterpreter;
2
3 public class JpythonScript {
4     public static void main(String args[]) {
5         PythonInterpreter interpreter = new PythonInterpreter();
6         interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
7         interpreter.exec("print days[1];");
8     }
9 }

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

Java程序中调用Python脚本的方法相关推荐

  1. 在Spark Scala/Java应用中调用Python脚本,会么?

    摘要:本文将介绍如何在 Spark scala 程序中调用 Python 脚本,Spark java程序调用的过程也大体相同. 本文分享自华为云社区<[Spark]如何在Spark Scala/ ...

  2. 教你如何在Spark Scala/Java应用中调用Python脚本

    摘要:本文将介绍如何在 Spark scala 程序中调用 Python 脚本,Spark java程序调用的过程也大体相同. 本文分享自华为云社区<[Spark]如何在Spark Scala/ ...

  3. Mac笔记本中是用Idea开发工具在Java项目中调用python脚本遇到的环境变量问题解决...

    问题描述: mac笔记本本身会自带几个python版本,比如python2.7版本,我没有改动mac默认的python版本,只是安装了python3.7版本. 使用Pycharm开发Python项目没 ...

  4. 如何在Java程序中调用Python算法脚本,重点讲Demo,不墨迹理论

    原创博文,欢迎转载,转载时请务必附上博文链接,感谢您的尊重. 前言 通过本篇,你将初步认识在Java程序中简单调用.py脚本文件的方法,附带入门的Demo实例讲解,更深入的理解还需要进一步学习. 最近 ...

  5. 解决C++中调用Python脚本的LNK2001错误

    解决C++中调用Python脚本的LNK2001错误 当我们尝试在C++程序中调用Python脚本时,有时会遇到"LNK2001:无法解析的外部符号__imp_Py_Initialize&q ...

  6. java执行python脚本_使用Runtime.getRuntime().exec()在java中调用python脚本

    举例有一个Python脚本叫test.py,现在想要在Java里调用这个脚本.假定这个test.py里面使用了拓展的包,使得pythoninterpreter之类内嵌的编译器无法使用,那么只能采用ja ...

  7. c++中调用python脚本提示 error LNK2001: 无法解析的外部符号 __imp_Py_Initialize等错误的解决方法

    c++中调用python脚本提示 error LNK2001: 无法解析的外部符号 __imp_Py_Initialize等错误的解决方法 时间:2017-05-09 12:32:06阅读:234评论 ...

  8. 本地方法(JNI)——从java 程序中调用C函数

    [0]README 1) 本文部分文字描述 转自 core java volume 2 , 旨在理解 本地方法--从java 程序中调用C函数 的基础知识 : 2) for source code, ...

  9. 【Python】如何在Excel中调用Python脚本,实现数据自动化处理

    这次我们会介绍如何使用xlwings将Python和Excel两大数据工具进行集成,更便捷地处理日常工作. 说起Excel,那绝对是数据处理领域王者般的存在,尽管已经诞生三十多年了,现在全球仍有7.5 ...

  10. 如何在Excel中调用Python脚本,实现数据自动化处理!

    大家好, 这次我们会介绍如何使用xlwings将Python和Excel两大数据工具进行集成,更便捷地处理日常工作. 说起Excel,那绝对是数据处理领域王者般的存在,尽管已经诞生三十多年了,现在全球 ...

最新文章

  1. 用python下载文件的若干种方法汇总
  2. 4 项目开发 - 后台
  3. 冒泡算法的三种JavaScript表示
  4. 《博德之门 3》究竟有多「DnD」?
  5. 【转】CMMI环境下,该如何实施Agile?
  6. GO string 转map_用go语言,只需两百行代码就能搞定并发聊天室!
  7. 前端遇到瓶颈怎么办?我来告诉你
  8. android expandablelistview横向,Android ExpandableListView使用小结(二)
  9. matlab符号函数作图,matlab符号函数的作图
  10. Django - 模型层 - 关系类型字段
  11. UIUC计算机科学系博士,伊利诺伊大学香槟分校计算机系统博士排名
  12. TOEFL wordlist 24
  13. 关于学术道德,我们应该遵循的规范
  14. Google-admob广告变现
  15. 宽带波束形成 恒定束宽波束形成 学习笔记
  16. Android保存搜索历史
  17. STM32定时器中断_立即触发解决方案
  18. ISCC2017 writeup
  19. Stm32f030数字电源开发 Stm32同步buck降压数字电源
  20. 抬头、低头、举手动作检测

热门文章

  1. 4. Javascript 函数
  2. Bootstraphead里的内容
  3. [2019杭电多校第四场][hdu6621]K-th Closest Distance(主席树)
  4. javascript把特定XML响应解析成一个对象
  5. Base64,DES,RSA,SHA1,MD5 笔记
  6. MySQL学习之一数据库简介
  7. 基于Docker布署伪分布式hadoop环境(一)
  8. Django中Settings中Templates的路径设置
  9. 数组元素循环右移问题
  10. PHP+Mysql+jQuery实现发布微博程序(PHP篇)