首页 > 编程语言 >java如何调用python.py文件并传参

java如何调用python.py文件并传参

时间:2023-07-11 13:34:59浏览次数:41  
标签:传参 testCaseCount java String keyword python xmindFilePath logger

注意: java调用python.py文件并传参,在windows和linux中使用是不一样的

我在windows操作系统中,java调用python文件并传参,是这样写的:完全没问题

try {
            Integer totalTestCaseCount = 0;
            //传入python文件的参数: String xmindFilePath, String testCaseKeyWord
            for (String keyword : testCaseKeyWord.split(",")) { //英文逗号作为分隔符号
                String parameterData = "python " + pythonFilePath + " \"" + xmindFilePath + "\" \"" + keyword + "\"";//因为xmindFilePath和keyword的值可能有空格,所以需要双引号
                Process process = Runtime.getRuntime().exec(parameterData);
                //获取Pyhton输出字符串 作为输入流被Java读取
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String s = null;
                Integer testCaseCount = 0;
                while ((s = in.readLine()) != null) {
                    //                System.out.println(s);
                    testCaseCount = Integer.valueOf(s);
                }
                logger.info("Get Test Case keyword {}'s count is {} from xmind file", keyword, testCaseCount);
                in.close();

                final int exitCode = process.waitFor();
                logger.info("Run python file result status is {}", exitCode);
                totalTestCaseCount += testCaseCount;
            }
            return totalTestCaseCount;
        } catch (IOException e) {
            logger.info(e.toString());
        } catch (InterruptedException e) {
            logger.info(e.toString());
        }

 

但是当代码放到linux服务器中去执行时,就不起作用了:

final int exitCode = process.waitFor()
获取到的exitCode 值就等于1(调用失败),但是我在linux服务器上去执行执行又一切正常~ 我就搞不懂原因了,所以换了下面这种方式去传参,就ok了,我也不知道为啥上面的方式在linux中就不失败了,如果走过路过的您知道原因,欢迎留言
try {
            Integer totalTestCaseCount = 0;
            //传入python文件的参数: String xmindFilePath, String testCaseKeyWord
            for (String keyword : testCaseKeyWord.split(",")) { //英文逗号作为分隔符号
                String pythonPath = "";
                String osName = System.getProperty("os.name");//判断操作系统
                if (osName.startsWith("Windows")) {
                    pythonPath = "python";
//                    parameterData = "python " + pythonFilePath + " \"" + xmindFilePath + "\" \"" + keyword + "\"";//因为xmindFilePath和keyword的值可能有空格,所以需要双引号
                } else {
                    pythonPath = "/usr/bin/python";
//                    parameterData = "/usr/bin/python " + pythonFilePath + " \"" + xmindFilePath + "\" \"" + keyword + "\"";//因为xmindFilePath和keyword的值可能有空格,所以需要双引号
                }
                String[] parameterData = new String[]{pythonPath, pythonFilePath, xmindFilePath, keyword};//第二个为python脚本所在位置,后面的为所传参数(得是字符串类型)
                logger.info("Python command is: {}", parameterData);
                Process process = Runtime.getRuntime().exec(parameterData);
                //获取Pyhton输出字符串 作为输入流被Java读取
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"gb2312"));//解决中文乱码,参数可传中文
                String line = null;
                Integer testCaseCount =0;
                while ((line = in.readLine()) != null) {
                    logger.info("The response data from python readFileUtil is :{}",line);
                    testCaseCount = Integer.valueOf(line);
                }
                in.close();
//方法二 // InputStreamReader stdin = new InputStreamReader(process.getInputStream()); // LineNumberReader input = new LineNumberReader(stdin); // String result = input.readLine(); // Integer testCaseCount = Integer.valueOf(result); // stdin.close(); // input.close(); logger.info("Get Test Case keyword {}'s count is {} from xmind file", keyword, testCaseCount); final int exitCode = process.waitFor(); logger.info("Run python file result status is {}, just 0 is normal status", exitCode); totalTestCaseCount += testCaseCount; } return totalTestCaseCount; } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); }

 

标签:传参,testCaseCount,java,String,keyword,python,xmindFilePath,logger
From: https://www.cnblogs.com/pingguomang/p/17544397.html

相关文章

  • Java网络编程
    1.ip和端口ip地址InetAddress//因为没有构造方法,所以不能通过new来生成对象,但是可以通过类名来调用类的静态方法InetAddressinetAddress1=InetAddress.getByName("localhost");System.out.println(inetAddress1);InetAddressinetAddress2=I......
  • python setup.py sdist bdist_wheel
    #pythonsetup.pysdistbdist_wheel#twineuploaddist/*importioimportosimportsysfromshutilimportrmtreefromsetuptoolsimportfind_packages,setup,CommandNAME='xgo-pythonlib'DESCRIPTION='PythonLibforXGO2-DOG'URL='h......
  • 520要通过这种方式告白 html+css+javascript canvas桃心代码 可修改 【附完整代码】
    ......
  • Python核对批量下载多个文件时的下载情况
      本文介绍批量下载遥感影像时,利用Python实现已下载影像文件的核对,并自动生成未下载影像的下载链接列表的方法。  批量下载大量遥感影像数据对于GIS学生与从业人员可谓十分常见。然而,对于动辄成千上万景的遥感影像文件,下载过程中可能会出现各类失败问题,且或许在下载软件或工具......
  • 【JAVA开发环境配置】 我也可以让JDK版本来去自由的切换了! 哈哈哈哈 舒服!
    ......
  • python pandas链接mysql数据库报错
    1. ModuleNotFoundError:Nomodulenamed'mysql'需要安装链接驱动,比如 pipinstallmysqlclient#或pipinstallpymysql#或pipinstallmysql-connector-python2.pandas的 pd.read_sql(ad_first_query,conn)报错: executereturnself.connectable.executio......
  • eclipse的java+tomcat配置以及一些异常处理
    真是折磨人,下载版本不匹配、匹配了又配置需要插件、插件下载后安装又出错误,运行时有报莫名其妙的错误……过程错了或者稍微忘了哪里就gg,等到最后还得查运行的bug。一、装jdk、jre,并配置环境变量系统变量→新建JAVA_HOME变量。系统变量→寻找Path变量→编辑,在变量值最后输入%JA......
  • Windows版python安装教程
    Windows版python安装教程第一步:下载Python最新版本Python官网:http://www.python.org进入官网之后,根据电脑系统选择合适的版本下载安装。截止到目前为止,python-3.10.2是Python最新版本(此版本Windows7系统及更早系统无法使用,Windows7系统的小伙伴可以选择下载python-3.8.10)。第......
  • Python - 打断点以及如何查看
    1、鼠标左键单击代码跟行号中间的地方会出现一个红点,这个就是断点。2、点击Debug按钮,进入调试模式。 3、当代码运行到断点之前,所有关于变量的代码,都会出现运行的结果。 4、点击StepIntoMyCode按钮,代码一步一步查看断点之后的内容。 5、最后:如果不想要继续查看断点......
  • 解决Python 画图 默认字体大小的具体操作步骤
    Python画图默认字体大小概述在Python中画图时,默认使用的字体大小可能不符合我们的需求,因此需要对字体大小进行调整。本文将介绍如何使用Python来实现画图时的默认字体大小,并提供详细的代码示例和解释。步骤下面是实现“Python画图默认字体大小”的步骤:步骤操作......