亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java執(zhí)行Python代碼的五種場景與示例方法

 更新時(shí)間:2025年07月29日 08:34:52   作者:yuanzhengme  
python可以搭建后端讓Java調(diào)用接口,但某些時(shí)候我們用到的python代碼可能并不多也許只有一個(gè)算法,此時(shí)就需要其他方法了,下面小編就來和大家詳細(xì)介紹一下吧

1.為什么

python擁有的某些庫要比Java強(qiáng)大,也擁有一些比Java更擅長的領(lǐng)域,python可以搭建后端讓Java調(diào)用接口,但某些時(shí)候我們用到的python代碼可能并不多也許只有一個(gè)算法,此時(shí)就需要以下方法了。

2.核心依賴

毫無疑問【自然是python的Java執(zhí)行器了】

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

3.使用

3.1類型一【直接執(zhí)行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類型二【執(zhí)行python文件后獲取返回結(jié)果】

1.無參數(shù)的python文件執(zhí)行

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

2.帶參數(shù)的python文件執(zhí)行

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

3.【W(wǎng)indows環(huán)境】使用bat腳本執(zhí)行python文件【我猜想也是有Linux環(huán)境的執(zhí)行方法的】

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;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

3.3類型三【讀取python文件內(nèi)的函數(shù)進(jìn)行執(zhí)行】

public class ExecPythonFileCode {
    public static void main(String[] args) {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile("D:\\PythonFile.py");
        PyFunction function = interpreter.get("add", PyFunction.class);
        int a = 3, b = 12;
        PyObject pyObject = function.__call__(new PyInteger(a), new PyInteger(b));
        System.out.println("The result is : " + pyObject);
        interpreter.close();
    }
}

4.python文件和執(zhí)行腳本

文件一:PythonFile.py

import numpy as np
a = np.arange(12).reshape(3,4)
print(a)
def add(a,b):
    return a+b;

文件二:PythonFileWithArgs.py

import sys
 
def func(a,b):
    return (a+b)
 
if __name__ == '__main__':
    a = []
    for i in range(1, len(sys.argv)):
        a.append((int(sys.argv[i])))
    print(func(a[0],a[1]))

文件三:RunPythonFile.bat

@echo off
cmd /k python E:\Anaconda3_Python\PythonFile.py

到此這篇關(guān)于Java執(zhí)行Python代碼的五種場景與示例方法的文章就介紹到這了,更多相關(guān)Java執(zhí)行Python代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論