java调用python脚本 并传参

java:

package com.mybatis.plus.utils;

import cn.hutool.core.lang.Console;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class test1 {
    public static void main(String[] args) {

        String[] arguments = new String[] {"python", "D://workPlace/git-project/py-test/MyTest2.py","9895656"};
        try {
            Process process = Runtime.getRuntime().exec(arguments);
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),
                    "GBK"));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            //java代码中的process.waitFor()返回值为0表示我们调用python脚本成功,
            //返回值为1表示调用python脚本失败,这和我们通常意义上见到的0与1定义正好相反
            int re = process.waitFor();
            System.out.println(re);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

python:

from enum import Enum
import sys

class Solution:
    def reverse(self, x: int) -> int:
        if x==0:
            return 0
        strList = []
        isPositive = True
        if x < 0:
            isPositive = False
        value = abs(x)
        temp = value
        while (temp/10.0>0.09):
            i = temp%10
            strList.append(str(i))
            temp = temp//10
        join = "".join(strList)
        result = int(join)
        if result>2147483647:
            result=0
        if isPositive == False:
            result = -result
        return result

if __name__ == "__main__":
    so = Solution()
    print("输入参数:"+sys.argv[1])
    print(so.reverse(int(sys.argv[1])))

 

执行java结果:

 

posted @ 2020-12-22 11:19  官萧何  阅读(937)  评论(0编辑  收藏  举报