Java:Java程序通过执行系统命令调用Python脚本

news/2025/3/12 12:40:27/

本文实现功能:Java程序调用Python脚本

Python脚本

import sysdef add(x, y):return x + yif __name__ == "__main__":print(add(int(sys.argv[1]), int(sys.argv[2])))

直接执行

$ python math.py 1 2
3

Java程序调用Python脚本

package io.github.mouday.utils;import java.io.*;/*** 执行系统命令*/
public final class ShellUtil {private ShellUtil() {}/*** 操作系统名称** @return Mac OS X*/public static String getOsName() {return System.getProperty("os.name");}/*** 判断是否为windows操作系统** @return*/public static Boolean isWindows() {return ShellUtil.getOsName().toLowerCase().contains("win");}/*** 根据系统返回对应平台执行命令* @param command* @return*/public static ProcessBuilder getProcessBuilder(String command) {if (ShellUtil.isWindows()) {return new ProcessBuilder("cmd", "/c", command);} else {return new ProcessBuilder("bash", "-c", command);}}/*** 执行系统命令* @param command* @return*/public static String executeCommand(String command) {try {ProcessBuilder pb = ShellUtil.getProcessBuilder(command);pb.redirectErrorStream(true);Process process = pb.start();process.waitFor();InputStream inputStream = process.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));String line;StringBuilder result = new StringBuilder();while ((line = reader.readLine()) != null) {result.append(line);// 换行符result.append(System.lineSeparator());}process.waitFor();inputStream.close();reader.close();process.destroy();return result.toString();} catch (InterruptedException | IOException e) {throw new RuntimeException(e);}}public static void main(String[] args) {// System.out.println(ShellUtil.executeCommand("echo 'hello'"));// helloSystem.out.println(ShellUtil.executeCommand("python math.py 1 2"));// 3}
}

参考视频

  1. JAVA开发必会小技巧8——用代码调用系统指令

http://www.ppmy.cn/news/994130.html

相关文章

133. 克隆图

给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆)。 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node])。 class Node { public int val; public List&…

【计算复杂性理论】证明复杂性(八):命题鸽巢原理(Propositional Pigeonhole Principle)的指数级归结下界

往期文章: 【计算复杂性理论】证明复杂性(Proof Complexity)(一):简介 【计算复杂性理论】证明复杂性(二):归结(Resolution)与扩展归结&#xff…

【C#常用操作】

excel相关操作 using Excel Microsoft.Office.Interop.Excel;public Excel.Application app; public Excel.Workbooks wbs; public Excel.Workbook wb; public Excel.Worksheets wss; public Excel.Worksheet ws;/// <summary> /// 取得打开excel句柄 /// </summary…

HCIE Security——防火墙互联技术

目录 一、防火墙接口互联接口 1.防火墙支持的接口及板卡 2.物理链接线缆 3.支持接口种类 &#xff08;1&#xff09;物理接口 &#xff08;2&#xff09;逻辑接口 二、相关配置命令 1.配置三层接口IP地址 2.配置PPPOE拨号接口 3.配置VLANIF接口、子接口、回环接口 4…

Sklearn-使用SVC对iris数据集进行分类

Sklearn-使用SVC对iris数据集进行分类 iris数据集的加载训练svc模型输出混淆矩阵和分类报告 使用SVC对iris数据集进行分类预测 涉及内容包含&#xff1a; 数据集的加载,训练集和测试集的划分训练svc模型,对测试集的预测输出混淆矩阵和分类报告使用Pipeline执行操作 iris数据集的…

平面设计软件都有哪些?推荐这7款

优秀的平面广告设计可以给产品带来良好的效益&#xff0c;正确传播品牌的价值和色调&#xff0c;而功能强大、使用方便的平面广告设计软件是创造优秀平面广告设计的关键。本文推荐7款备受好评的平面广告设计软件&#xff0c;易于使用&#xff01; 1.即时设计 即时设计是国内一…

文件IO练习

一、用read函数完成文件大小计算 #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main(int argc, const char *argv[]) {int fd open("./1.tx…

HCIP OSPF+BGP综合实验

题目 1、该拓扑为公司网络&#xff0c;其中包括公司总部、公司分部以及公司骨干网&#xff0c;不包含运营商公网部分。 2、设备名称均使用拓扑上名称改名&#xff0c;并且区分大小写。 3、整张拓扑均使用私网地址进行配置。 4、整张网络中&#xff0c;运行OSPF协议或者BGP协议…