jpython的使用(Java调用python脚本)

1、导入Jar

jar下载地址

找到jython-installer-2.7.0.jar 包里面的jpython.jar,加载到工作目录

我用的java开发工具是IDEA、JDK8、Python3.5

2 、执行python源码

获取一个元组里面的元素

import org.python.util.PythonInterpreter;
public class FirstJavaScript {
    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];");

    }// main
}

一般会出现以下异常

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: ['C:\\Users\\Administrator\\IdeaProjects\\MyText\\lib\\Lib', '__classpath__', '__pyclasspath__/']
    This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: C:\Users\Administrator\IdeaProjects\MyText\lib
    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

看错误可以理解为一些库的路径错误,下面也给出了解决方法

我们只需要吧代码改为以下这个例子就可以顺利执行了。

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];");

执行结果是Tue

3、调用.py中的方法

test.py的源码

def add(a, b):
    return a + b

Java源码

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.execfile("test.py");
     PyFunction func = (PyFunction) interpreter.get("adder",
             PyFunction.class);

     int a = 100, b = 100;
     PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
     System.out.println("anwser = " + pyobj.toString());

执行结果为 200

4、执行 .py文件

test.py

# -*- coding: utf-8 -*

print ("hello")
ls = [1,2,3,4,5,6]
print(ls)
print('你好')

java代码


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.execfile("test.py");


执行结果:

hello
[1, 2, 3, 4, 5, 6]
你好

如果报出如下异常,请在源码加上
# -- coding: utf-8 -

Exception in thread "main" SyntaxError: Non-ASCII character in file
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 传送门 解读阿里Java开发手册(v1.1.1) - 异常日志 前言 阿里Java开发手册谈不上圣经,但确实是大量...
    kelgon阅读 4,410评论 4 50
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,834评论 18 399
  • (一)Java部分 1、列举出JAVA中6个比较常用的包【天威诚信面试题】 【参考答案】 java.lang;ja...
    独云阅读 7,151评论 0 62
  • 80/20 法则 (二八法则) 世界80% 的财富来自于20%的人. 80/20 法则 (二八法则) 是由意大利经...
    zhoujia阅读 434评论 0 0
  • 夕阳归途斜 游花未尽赏 山无闹声静 溪入村烟明
    凌峰峰行阅读 194评论 0 3