Java【代码分享 07】Java执行Python代码的3类5种情况测试(Java源码+Python源码举例)_java 执行python 如何判断执行是否成功

1.why

python拥有的某些库要比Java强大,也拥有一些比Java更擅长的领域,python可以搭建后端让Java调用接口,但某些时候我们用到的python代码可能并不多也许只有一个算法,此时就需要以下方法了。

2.核心依赖

毫无疑问【自然是python的Java执行器了】

<dependency>
	<groupId>org.python</groupId>
	<artifactId>jython-standalone</artifactId>
	<version>2.7.0</version>
</dependency>

3.使用

3.1类型一【直接执行python代码】
public class ExecPythonCode {
    public static void main(String[] args) {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("a=[5,2,3,9,4,0];");
        // 此处python语句是3.x版本的语法
        interpreter.exec("print(sorted(a));"); 
        // 此处是python语句是2.x版本的语法
        interpreter.exec("print sorted(a);"); 
        interpreter.close();
    }
}

3.2类型二【执行python文件后获取返回结果】
  1. 无参数的python文件执行
public class ExecPythonFile {
    public static void main(String[] args) {
        try {
            Process process = Runtime.getRuntime()
                    .exec("python D:\\PythonFile.py");
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

  1. 带参数的python文件执行
public class ExecPythonFileWithArgs {
    public static void main(String[] args) {
        int a = 18, b = 19;
        args = new String[] { "python","D:\\PythonFileWithArgs.py",
        String.valueOf(a), String.valueOf(b) };
        try {
            Process process = Runtime.getRuntime().exec(args);
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

  1. 【Windows环境】使用bat脚本执行python文件【我猜想也是有Linux环境的执行方法的】
public class ExecPythonBat {
    public static void main(String[] args) {
        try {
            Process process = Runtime.getRuntime().exec("cmd /c D:\\RunPythonFile.bat");
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;


![img](https://img-blog.csdnimg.cn/img_convert/505f5bd8bed2fa03c361ff33a41ea6bb.png)
![img](https://img-blog.csdnimg.cn/img_convert/5e121726990275ef09219316b1a450e4.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

lOK-1726075290324)]
[外链图片转存中...(img-RBeFq9vc-1726075290325)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值