[JAVA]Runtime 调用Python脚本(可传参)

使用场景:在java中通过runtime调用python脚本

1.python脚本中连接Oracle数据库查询数据后输出文件,脚本头内容如下

#!/usr/bin/python3
#encoding=utf-8
import os
from pypinyin import pinyin,lazy_pinyin
import cx_Oracle as oracle
import sys
import pypinyin
import datetime
import redis
import argparse

os.environ["ORACLE_HOME"] = '/Users/apple/Library/Oracle/instantclient_12_2'
os.environ["DYLD_LIBRARY_PATH"] = '/Users/apple/Library/Oracle/instantclient_12_2'
os.environ["LD_LIBRARY_PATH"] = '/Users/apple/Library/Oracle/instantclient_12_2'
os.environ["NLS_LANG"] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
#定义脚本说明文案
parser = argparse.ArgumentParser(description='-updateuser:操作用户名')
#定义脚本入参
parser.add_argument('-uu', type=str,default=None)

脚本下面的内容都是查询数据库,然后将查询结果输出为文件,就略过了。再补充一下main方法的内容

if __name__ == '__main__':
    args = parser.parse_args()
    print(args.uu)
    cc = ChineseConvertPinYin()
    cc.execute(args.uu)

2.Java中通过Runtime调用执行python

//设置java执行进程的环境变量(临时的)

String[] envp = new String[]{"LD_LIBRARY_PATH=/Users/apple/Library/Oracle/instantclient_12_2","LANG=UTF-8"};

//exec方法 第一个参数是执行的命令,第二个参数为环境变量数据(环境变量设置方式:NAME=VALUE)

Process pr=Runtime.getRuntime().exec("python3 /Work/pythonWork/pinyin/ChineseConvertPinYin.py -uu wzj",envp);

System.out.println(pr.waitFor());//返回0表示执行成功,返回1表示执行失败(PS:网上看到有数如果执行多条命令的时候,这个waitFor必不可少,本次没有实际用途,暂未深入研究)

//获取输出流,即:python脚本中的print 或 shell 的echo

BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));

String line = null;

while((line = in.readLine()) != null){

System.out.println(line);//输出每一行打印或echo

}

//获取异常输出流

BufferedReader ine = new BufferedReader(new InputStreamReader(pr.getErrorStream()));

String linee = null;

while((linee = ine.readLine()) != null){

System.out.println(linee);

}

3.总结 

过程中遇到过两个问题:

1.Python调用Oracle连接驱动的问题,使用pip安装cx_Oracle后,总是报错,大致内容如下:

cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "dlopen(libclntsh.dylib, 1): image not found". See https://oracle.github.io/odpi/doc/installation.html#macos for help

解决方式:安装oracle连接驱动,此处参考 http://jashawn.iteye.com/blog/1654441

2.Java Runtime执行的时候,Python脚本中的临时环境变量未生效(此处有待后续研究),遂在java进程中设置独立环境变量,代码如下:

String[] envp = new String[]{"LD_LIBRARY_PATH=/Users/apple/Library/Oracle/instantclient_12_2","LANG=UTF-8"}; 

3.补充一下,linux or mac下安装oracle驱动包以及环境变量配置(以下代码来源网络)

3.1下载所需要的驱动包

wget https://github.com/bumpx/oracle-instantclient/raw/master/instantclient-basic-linux.x64-11.2.0.4.0.zip -O /tmp/instantclient-basic-linux.x64-11.2.0.4.0.zip && \

wget https://github.com/bumpx/oracle-instantclient/raw/master/instantclient-sdk-linux.x64-11.2.0.4.0.zip -O /tmp/instantclient-sdk-linux.x64-11.2.0.4.0.zip 

3.2解压到指定目录

unzip /tmp/instantclient-basic-linux.x64-11.2.0.4.0.zip -d /usr/local/ && \

unzip /tmp/instantclient-sdk-linux.x64-11.2.0.4.0.zip -d /usr/local/

3.3建立软连

ln -s /usr/local/instantclient_11_2 /usr/local/instantclient && \

ln -s /usr/local/instantclient/libclntsh.so.11.1 /usr/local/instantclient/libclntsh.so

3.4配置环境变量

echo "LD_LIBRARY_PATH=/usr/local/instantclient:$LD_LIBRARY_PATH" >> .bash_profile

echo "PATH=$PATH:/usr/local/instantclient" >> .bash_profile

echo "export LD_LIBRARY_PATH PATH" >> .bash_profile

3.5使环境变量生效

source .bash_profile 

你可能感兴趣的:(java,python3,cx_Oracle,环境变量,java,Runtime)