zoukankan      html  css  js  c++  java
  • (转)java调用python脚本

    这篇博客旨在吐血分享今天遇到的java调用python脚本遇到的坑,折腾了3个多小时终于可以跑通了,代码超级短,但网上的好多资料都是抄来抄去的,很少有能够直接跑通的,尤其是针对你的python文件中用到第三方类库的情况。

    先来说说我为什么要用java调用python代码,原因就在于python在爬虫方面提供了丰富的类库,但我本来已经有一套java操作数据库以及消息中间件的代码,这些代码用python实现的话可能需要至少一周时间,为了减少时间成本,因此我决定用java调用python脚本的方式使用requests等等库,下面把我解决的过程分享出来,希望能给遇到类似问题的朋友一点帮助。

    常见的java调用python脚本方式

    • 通过jython.jar提供的类库实现
    • 通过Runtime.getRuntime()开启进程来执行脚本文件

    通过jython.jar提供的类库实现

    通过jython.jar实现的话,我们需要引入jar包,具体我写了一个demo,假设你的python代码为test.py:

    def my_test(name, age):
        print("name: "+name)
        print("age: "+age)
        return "success"

    java调用test.py代码:

    public static void main(String[] args) {
          PythonInterpreter interpreter = new PythonInterpreter();  
          interpreter.execfile("E:\workspace\pycharm_workspace\weixincrawer\test.py");  
          PyFunction function = (PyFunction)interpreter.get("my_test",PyFunction.class);  
          PyObject pyobject = function.__call__(new PyString("huzhiwei"),new PyString("25")); 
          System.out.println("anwser = " + pyobject.toString());  
        }

    输出结果:

    name: huzhiwei
    age: 25
    anwser = success

    到此是没有什么问题的,我们使用function.call方法传入参数调用python函数,使用pyobject.toString()方法拿到python中my_test函数的返回值,但是如果你把test.py稍微做下修改如下:

    import requests
    
    def my_test(name, age):
        response = requests.get("http://www.baidu.com")
        print("name: "+name)
        print("age: "+age)
        return "success"

    不修改java调用代码的情况下,你会得到下面异常信息:

    ImportError: No module named requests

    没错,这就是我要讨论的问题,因为jython不可能涵盖所有python第三方类库的东西,所以在我们得python文件中用到requests类库的时候,很显然会报找不到模块的错误,这个时候我们是可以通过Runtime.getRuntime()开启进程来执行python脚本文件的。

    通过Runtime.getRuntime()开启进程来执行脚本文件

    使用这种方式需要同时修改python文件以及java调用代码,在此我同样在上面test.py的基础上进行修改:

    import requests
    import sys
    
    def my_test(name, age):
        response = requests.get("http://www.baidu.com")
        print("url:"+response.url)
        print("name: "+name)
        print("age: "+age)
        return "success"
    
    my_test(sys.argv[1], sys.argv[2])

    和上面test.py代码最大的区别在于,我们此处开启进程的方式实际上是在隐形的调用dos界面进行操作,因此在python代码中我们需要通过sys.argv的方式来拿到java代码中传递过来的参数。

    java调用代码部分:

    public static void main(String[] args) {
          String[] arguments = new String[] {"python", "E:\workspace\pycharm_workspace\weixincrawer\test.py", "huzhiwei", "25"};
            try {
                Process process = Runtime.getRuntime().exec(arguments);
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = null;  
              while ((line = in.readLine()) != null) {  
                  System.out.println(line);  
              }  
              in.close();  
              int re = process.waitFor();  
              System.out.println(re);
            } catch (Exception e) {
                e.printStackTrace();
            }  
        }

    结果输出:

    url:http://www.baidu.com/
    name: huzhiwei
    age: 25
    0

    在此需要注意的一点,java代码中的process.waitFor()返回值为0表示我们调用python脚本成功,返回值为1表示调用python脚本失败,这和我们通常意义上见到的0与1定义正好相反。

    我的代码:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Date    : 2018-02-27
    # @Author  : zhangmingcheng
    
    from PIL import Image
    import imagehash
    import sys
    
    def get_hash(path):
        image = Image.open(path)
        h = str(imagehash.dhash(image))
        print h
        return h 
    
    get_hash(sys.argv[1])
    

    java

    	/**
    	 * 获取影像dhash,获取完后将影像删除
    	 */
    	@Override
    	public String getDhash(String absolutePathName) {
    		String dhash = "";
    		String[] arguments = new String[] { "python", "/root/imagedhash/rest.py", absolutePathName };
    		try {
    			Process process = Runtime.getRuntime().exec(arguments);
    			BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    			String line = null;
    			while ((line = in.readLine()) != null) {
    				dhash = line;
    			}
    			in.close();
    			process.waitFor();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		File file = new File(absolutePathName);
    		if (file.exists() && file.isFile()) {
    			file.delete();
    			System.out.println(absolutePathName + "影像的dhash值=" + dhash + ",删除影像成功");
    		}
    		return dhash;
    	}

    转自:http://blog.csdn.net/hzw19920329/article/details/77509497

  • 相关阅读:
    架构阅读笔记9
    架构阅读笔记8
    架构阅读笔记7
    架构阅读笔记6
    架构阅读笔记5
    git安装使用详解
    mysql主从
    人定胜天
    jquery即点击改
    无限极分类/数组处理
  • 原文地址:https://www.cnblogs.com/zhangmingcheng/p/8479564.html
Copyright © 2011-2022 走看看