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

发布时间 2023-07-11 13:23:54作者: 苹果芒

注意: 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(); }