maven-3-java调用python程序

【Java】使用Java调用Python的四种方法
fastjson的使用——JSON字符串、JSON对象、Java对象的互转
使用idea给Java程序打jar包(超简单 超详细)
java 使用Process调用exe程序 及 Process.waitFor() 死锁问题了解和解决

1 环境准备

(1)新建Maven类型的项目,D:\processPython。
右键src/main/java,创建java类。

(2)在使用Java调用python之前,需要导入依赖环境。
如,在maven项目中,需要导入如下依赖:
目前只有2.7.X版本有相关依赖包。

<dependencies>
        <dependency>
            <groupId>org.python</groupId>
            <artifactId>jython-standalone</artifactId>
            <!--python版本是2.x还是3.x在这里指定-->
            <version>2.7.1</version>
        </dependency>
    </dependencies>

2 调用

2.1 在java类中直接执行python语句

创建Java类JavaRunPython。

import org.python.util.PythonInterpreter;
public class JavaRunPython {
    public static void main(String[] args) {
        //首先调用python的解释器
        PythonInterpreter interpreter = new PythonInterpreter();
        //选择执行的的Python语句
        interpreter.exec("import sys; ");
        interpreter.exec("a='hello world123'; ");
        interpreter.exec("print(sys.version,a);");
    }
}
输出如下:
('2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43) 
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)]',
'hello world123')

在正常的情况下,Java工程师只需要调用python开发工程师写的脚本就行了,不会再在代码中加入python语句了。因为在Java语言中添加入其他语言,使Java的可读性下降、执行意义也不是很大。

2.2 在java中直接调用python脚本

这种方式是有些Java程序员使用调用。
(1)首先将准备好的脚本,如plus.py。

import sys
a = 100
b = 200
print(sys.version,a+b)

(2)创建java类JavaPythonFile

import org.python.util.PythonInterpreter;
public class JavaPythonFile {
    public static void main(String[] args) {
        PythonInterpreter interpreter = new PythonInterpreter();
        //我在这里使用绝对路径
        interpreter.execfile("D:\\processPython\\src\\main\\java\\plus.py");
    }
}
输出如下:
('2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43) 
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)]', 300)

2.3 用Runtime.getRuntime()执行python脚本

这种办法可以让进程与程序交互,可以看到出现的问题,便于解决。
(1)首先将准备好的脚本,如plus.py。

a = 100
b = 200
print(a+b)

(2)创建java类RuntimeFunction

2.3.1 方式一系统默认python

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RuntimeFunction {
    public static void main(String[] args) {
        Process proc;
        try {
            proc = Runtime.getRuntime().exec("python D:\\processPython\\src\\main\\java\\plus.py");
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
输出如下
3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] 300

查看系统的python版本为3.7.4。
在这里插入图片描述

2.3.2 方式二指定python

String[] args1=new String[]{"/home/huan/anaconda2/bin/python","/home/huan/myfile/helloword.py"};
Process pr=Runtime.getRuntime().exec(args1);

附加:String数组里的那一行很重要。
首先一定要设置好你所使用的python的位置,切记不要直接使用python,因为系统会默认使用自带的python,所以一定要设置好你所使用的python的位置,否则可能会出现意想不到的问题(比如说我使用的是anaconda中的python,而ubuntu系统会默认调用自带的python,而我自带的python中并没有numpy库,所以会造成相应的代码不会执行的问题,所以设置好python的位置是很重要的)。还有就是要设置好py文件的位置,使用绝对路径。还有就是可以看出,此方法可以满足我们python代码中调用第三方库的情况,简单实用。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RuntimeFunction {
    public static void main(String[] args) {
        Process proc;
        try {
            String[] args1=new String[]{"D:\\Anaconda3\\envs\\python38\\python","D:\\processPython\\src\\main\\java\\plus.py"};
            proc = Runtime.getRuntime().exec(args1);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

首先,为什么要创建Process proc。ProcessBuilder.start()和Runtime.exec两个方法会生成一个本地的进程,然后返回一个Processs子类的实例。通过这个实例可以控制进程以及获得关于进程的信息。这个Process类为进程提供可用于执行来自的输入的方法,然后执行输出到这个进程中,等待完成后,检查进程退出时的状态,然后停止这个进程。——换句话说,这个实例就是监视整个进程的。

通过Process的getInputStream(),getOutputStream()和getErrorStream()方法可以得到输入输出流,然后通过InputStream可以得到程序对控制台的输出信息,通过OutputStream可以给程序输入指令,这样就达到了程序的交换功能。

3 java处理json

如果使用的是maven,需要在pom.xml文件中加入依赖:

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.78</version>
</dependency>

3.1 json字符串-简单对象

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class JavaJsonProcess {
    public static void main(String[] args) {
        //json字符串-简单对象
        String jsonStr = "{\"studentName\":\"张三\",\"studentAge\":18}";
        //简单对象
        JSONObject jsonObj = JSON.parseObject(jsonStr);
        System.out.println(jsonStr);
        System.out.println(jsonObj);
    }
}

3.2 json字符串-数组类型

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class JavaJsonProcess {
    public static void main(String[] args) {
        //json字符串-数组类型
        String jsonArrStr = "[{\"studentName\":\"张三\",\"studentAge\":18},{\"studentName\":\"李四\",\"studentAge\":17}]";
        //数组类型
        JSONArray jsonArray = JSON.parseArray(jsonArrStr);
        //遍历JSONArray方法1
        for(int i = 0; i < jsonArray.size(); i++){
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            System.out.println(jsonObj);
        }

        //遍历JSONArray方法2
        for(Object obj : jsonArray){
            JSONObject jsonObject = (JSONObject) obj;
            System.out.println(jsonObject);
        }
	}
}

3.3 json字符串-复杂对象

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class JavaJsonProcess {
    public static void main(String[] args) {
        //json字符串-复杂对象
        String complexJsonStr= "{\"teacherName\":\"李寻欢\",\"teacherAge\":30,\"course\":{\"courseName\":\"武术\",\"code\":110},\"students\":[{\"studentName\":\"张三\",\"studentAge\":18},{\"studentName\":\"李四\",\"studentAge\":19}]}";
        //复杂对象
        JSONObject jsonObj = JSON.parseObject(complexJsonStr);
        //取出复杂对象中各项内容
        String teacherName = jsonObj.getString("teacherName");
        Integer teacherAge = jsonObj.getInteger("teacherAge");
        JSONObject course = jsonObj.getJSONObject("course");
        JSONArray students = jsonObj.getJSONArray("students");
        System.out.println(teacherName);
        System.out.println(teacherAge);
        System.out.println(course);
        System.out.println(students);
    }
}
输出如下:
李寻欢
30
{"courseName":"武术","code":110}
[{"studentAge":18,"studentName":"张三"},{"studentAge":19,"studentName":"李四"}]

4 方式一:传递json参数

用sys.argv接收参数。

4.1 依赖引用pom.xml

如果使用的是maven,需要在pom.xml文件中加入依赖:

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.78</version>
</dependency>

4.2 配置文件pythonuse.properties

在这里插入图片描述

#python.path = python
python.path = D:\\Anaconda3\\envs\\python38\\python33
python.package = C:\\Users\\user\\Desktop\\aa_main.py

4.3 代码RuntimeFunction.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class RuntimeFunction {
    public static void main(String[] args) {
        Process proc;
        try {
            //通过java反射机制获取配置文件
            InputStream is = RuntimeFunction.class.getClassLoader().getResourceAsStream("pythonuse.properties");
            Properties pro = new Properties();
            try {
                pro.load(is);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            String pythonpath = pro.getProperty("python.path");
            String pythonpackage = pro.getProperty("python.package");

            //json字符串-简单对象
            String jsonStr = "{\"name\":\"张三\",\"time\":\"2022-12-10 16:16:45.256\",\"age\":18}";
            System.out.println(jsonStr);
            //简单对象
            JSONObject jsonObj = JSON.parseObject(jsonStr);
            //简单对象转换为字符串
            String re_str = jsonObj.toJSONString();
            System.out.println(re_str);
            //将“替换成\"
            re_str = re_str.replaceAll("\"","\\\\\"");
            System.out.println(re_str);

            //String[] args1=new String[]{"D:\\Anaconda3\\envs\\python38\\python","C:\\Users\\user\\Desktop\\aa_main.py",re_str};
            String[] args1=new String[]{pythonpath,pythonpackage,re_str};
            //System.out.println(args1);
            proc = Runtime.getRuntime().exec(args1);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                JSONObject jsonObj1 = JSON.parseObject(line);
                System.out.println(jsonObj1);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


4.4 代码aa_main.py

# -*- coding: UTF-8 -*-
import sys
import json
import sys
if __name__ == "__main__":
    # 1 当作命令行参数,容易出现时间戳中的空格,被拆分成了多个参数
    receive_msg = " ".join(sys.argv[1:]) # 用空格把分隔的参数合并起来
    # 2 解析
    receive_dict = json.loads(receive_msg)
    # print(receive_dict["name"],receive_dict["age"],receive_dict["time"])
    out_dict = {
        "接收到":receive_dict
    }
    # 3 序列化
    msg = json.dumps(out_dict)
    msg = msg.replace('"','\"')
    print(msg)

4.5 打包和运行

(1)打包后会自动将配置文件加载到与类文件同一级目录
在这里插入图片描述
(2)执行命令

java -cp processPython.jar  RuntimeFunction main

在这里插入图片描述

5 方式二:传递json参数

将json参数传递到控制台的命令行。

5.1 代码RuntimeFunction.java

import java.io.*;
import java.util.Properties;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class RuntimeFunction {
    public static void main(String[] args) {
        Process proc;
        try {
            //通过java反射机制获取配置文件
            InputStream is = RuntimeFunction.class.getClassLoader().getResourceAsStream("pythonuse.properties");
            Properties pro = new Properties();
            try {
                pro.load(is);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            String pythonpath = pro.getProperty("python.path");
            String pythonpackage = pro.getProperty("python.package");

            //json字符串-简单对象
            String jsonStr = "{\"name\":\"张三\",\"time\":\"2022-12-10 16:16:45.256\",\"age\":18}";
            System.out.println(jsonStr);
            //简单对象
            JSONObject jsonObj = JSON.parseObject(jsonStr);
            //简单对象转换为字符串
            String re_str = jsonObj.toJSONString();
            System.out.println(re_str);

            //String[] args1=new String[]{"D:\\Anaconda3\\envs\\python38\\python","C:\\Users\\user\\Desktop\\aa_main.py",re_str};
            String[] args1=new String[]{pythonpath,pythonpackage};
            proc = Runtime.getRuntime().exec(args1);
            // 向控制台的命令行中写入参数
            PrintStream ps = new PrintStream(proc.getOutputStream());
            ps.print(re_str);
            ps.flush();
            ps.close();

            // 从控制台的命令行读取返回的参数
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                JSONObject jsonObj1 = JSON.parseObject(line);
                System.out.println(jsonObj1);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

5.2 代码aa_main.py

# -*- coding: UTF-8 -*-
import sys
import json
import sys
if __name__ == "__main__":
    # 方式二:直接从命令行
    x = sys.stdin
    for line in x:
        # 2 解析
        receive_dict = json.loads(line)
        # print(receive_dict["name"],receive_dict["age"],receive_dict["time"])
        out_dict = {
            "接收到":receive_dict
        }
        # 3 序列化
        msg = json.dumps(out_dict,ensure_ascii=False)
        print(msg)
        break

6 打包运行

6.1 打包

1.file ---> project structure --->Artifacts--->+ --->JAR--->
From  modules with dependencies...---> Main class(选择启动类)--->
OK--->apply --->OK 
2.build ---> build Artifacts--->xxx.jar ---> build

6.2 运行

直接通过命令指定运行,运行时指定的main方法:

java -cp huobi-client.jar com.huobi.start.LoadPrice main
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
xxl-job是一个分布式任务调度平台,可以用于JavaPython等多种语言的任务调度。下面我将分别介绍JavaPython如何调用xxl-job。 Java调用xxl-job的步骤如下: 1. 在Java项目中引入xxl-job的依赖。可以通过Maven或者Gradle等构建工具添加以下依赖: ```xml <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.3.0</version> </dependency> ``` 2. 在Java代码中编写任务执行逻辑。需要实现xxl-job提供的`IJobHandler`接口,并重写`execute`方法,该方法即为任务的执行逻辑。 ```java public class MyJobHandler extends IJobHandler { @Override public ReturnT<String> execute(String param) throws Exception { // 任务执行逻辑 // ... return ReturnT.SUCCESS; } } ``` 3. 在xxl-job的管理后台配置任务。登录xxl-job的管理后台,创建一个任务,并配置任务的执行器为Java任务,同时指定任务的执行类为上一步编写的`MyJobHandler`。 4. 启动xxl-job的执行器。在Java项目中添加一个启动类,通过调用`XxlJobExecutor`的`init`方法来启动执行器。 ```java public class XxlJobExecutorSample { public static void main(String[] args) { XxlJobExecutor.init(); } } ``` 5. 启动Java项目,执行器会自动注册到xxl-job的调度中心,等待调度中心分配任务并执行。 Python调用xxl-job的步骤如下: 1. 安装xxl-job的Python客户端。可以通过pip安装: ``` pip install xxl-job-client ``` 2. 在Python代码中编写任务执行逻辑。需要导入xxl-job的Python客户端,并使用`@job_handler`装饰器来标记任务执行函数。 ```python from xxl_job import job_handler @job_handler def my_job_handler(param): # 任务执行逻辑 # ... return "SUCCESS" ``` 3. 在xxl-job的管理后台配置任务。登录xxl-job的管理后台,创建一个任务,并配置任务的执行器为Python任务,同时指定任务的执行函数为上一步编写的`my_job_handler`。 4. 启动Python脚本,执行器会自动注册到xxl-job的调度中心,等待调度中心分配任务并执行。 希望以上步骤可以帮助到你,如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮冰燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值