
相关免费学习推荐:python视频教程
java调用python的几种用法如下:
- 在java类中直接执行python语句
- 在java类中直接调用本地python脚本
- 使用Runtime.getRuntime()执行python脚本文件(推荐)
- 调用python脚本中的函数
准备工作:
创建maven工程,结构如下:

到官网https://www.jython.org/download.html下载Jython的jar包或者在maven的pom.xml文件中加入如下代码:
1 2 3 4 5 | <dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
|
Nach dem Login kopieren
1.在java类中直接执行python语句
创建JavaRunPython.java类:
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.test;
import org.python.util.PythonInterpreter;
public class JavaRunPython {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter. exec ( "a='hello world'; " );
interpreter. exec ( "print a;" );
}
}
|
Nach dem Login kopieren
输出结果如下:

出现的console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.并不是错误,而是兼容所导致,解决方法如下:



2.在java中直接调用python脚本
在本地的D盘创建一个python脚本,文件名字为javaPythonFile.py,文件内容如下:
创建JavaPythonFile.java类,内容如下:
1 2 3 4 5 6 7 8 9 10 11 | package com.test;
import org.python.util.PythonInterpreter;
public class JavaPythonFile {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile( "D:\\javaPythonFile.py" );
}
}
|
Nach dem Login kopieren
输出结果如下:

3.使用Runtime.getRuntime()执行python脚本文件,推荐使用
在本地的D盘创建一个python脚本,文件名字为Runtime.py,文件内容如下:
1 | print ('RuntimeDemo')
|
Nach dem Login kopieren
创建RuntimeFunction.java类,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.test;
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:\\Runtime.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();
}
}
}
|
Nach dem Login kopieren
运行结果如下:

4.调用python脚本中的函数
在本地的D盘创建一个python脚本,文件名字为add.py,文件内容如下:
1 2 | def add(a,b):
return a + b
|
Nach dem Login kopieren
创建Function.java类,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.test;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class Function {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile( "D:\\add.py" );
PyFunction pyFunction = interpreter.get( "add" , PyFunction. class );
int a = 5, b = 10;
PyObject pyobj = pyFunction.__call__( new PyInteger(a), new PyInteger(b));
System.out.println( "the anwser is: " + pyobj);
}
}
|
Nach dem Login kopieren
运行结果如下:

到此这篇关于详解java调用python的几种用法(看这篇就够了)的文章就介绍到这了。
相关免费学习推荐:php编程(视频)
Das obige ist der detaillierte Inhalt vonDetaillierte Erläuterung der Verwendung von Java-Aufrufen in Python. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!