从 Java 调用 Python 代码时出现问题(不使用 jython)
Issue in calling Python code from Java (without using jython)
我发现这是从 java 运行(使用 exec() 方法)python 脚本的方法之一。我在 python 文件中有一个简单的打印语句。但是,当我 运行 它时,我的程序什么也不做。它既不打印写在 python 文件中的语句,也不抛出异常。程序什么都不做就终止了:
Process p = Runtime.getRuntime().exec("C:\Python\Python36-32\python.exe C:\test2.py");
即使这样也没有创建输出文件:
Process p = Runtime.getRuntime().exec("C:\Python\Python36-32\python.exe C:\test2.py output.txt 2>&1");
问题是什么?
我想您可以试试 ProcessBuilder class 的运气。
如果我正确阅读了 Oracle 文档,std 输入和输出默认定向到管道但是ProcessBuilder 有一个简单的方法供您显式set output (or input) to a file on your system or something else .
如果您希望 Python 程序使用与 Java 程序相同的输出(可能是 stdout 和 stderr),您可以像这样使用 stg:
ProcessBuilder pb = new ProcessBuilder("C:\Python\Python36-32\python.exe", "C:\test2.py");
pb.redirectOutput(Redirect.INHERIT);
Process p = pb.start();
启动 python 进程的一种方法是使用入口点 - test.cmd
echo Hello
python hello.py
这里是hello.py
#!/usr/bin/env python3
import os
if not os.path.exists('dir'):
os.makedirs('dir')
这是我的 Java 代码:
public static void main(String[] args) throws IOException {
try {
Process p = Runtime.getRuntime().exec("test.cmd");
p.waitFor();
Scanner sc = new Scanner(p.getInputStream());
while(sc.hasNextLine()){
System.out.println(sc.nextLine());
}
sc.close();
} catch (Exception err) {
err.printStackTrace();
}
}
您可以使用 ProcessBuilder API,将输出重定向到文件,然后等待结果。
public class Main {
public static final String PYTHON_PATH = "D:\Anaconda3\python.exe";
public static final String PATH_TO_SCRIPT = "D:\projects\StartScript\test.py";
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder();
builder.command(PYTHON_PATH, PATH_TO_SCRIPT);
// Redirect output to a file
builder.redirectOutput(new File("output.txt"));
builder.start().waitFor();
// Print output to console
ProcessBuilder.Redirect output = builder.redirectOutput();
File outputFile = output.file();
BufferedReader br = new BufferedReader(new FileReader(outputFile));
String st;
while ((st = br.readLine()) != null) {
System.out.println(st);
}
}
}
python 文件 test.py 包含一个简单的打印语句:
print("Hello from python")
如果不用等结果的话我想会更简单
使用过程 API 也应该有效。
就像你的例子(我使用上面声明的相同常量):
Process p = Runtime.getRuntime().exec(PYTHON_PATH + " " + PATH_TO_SCRIPT);
p.waitFor();
byte[] buffer = new byte[1024];
byte[] errBuffer = new byte[1024];
p.getInputStream().read(buffer);
p.getErrorStream().read(errBuffer);
System.out.println(new String(buffer));
System.out.println(new String(errBuffer));
要查看打印语句的输出,您需要等待并重定向流。错误流也一样。
现在,如果你像这样破坏 python 脚本:
print("Hello from python')
您应该也能看到打印的错误。
我发现这是从 java 运行(使用 exec() 方法)python 脚本的方法之一。我在 python 文件中有一个简单的打印语句。但是,当我 运行 它时,我的程序什么也不做。它既不打印写在 python 文件中的语句,也不抛出异常。程序什么都不做就终止了:
Process p = Runtime.getRuntime().exec("C:\Python\Python36-32\python.exe C:\test2.py");
即使这样也没有创建输出文件:
Process p = Runtime.getRuntime().exec("C:\Python\Python36-32\python.exe C:\test2.py output.txt 2>&1");
问题是什么?
我想您可以试试 ProcessBuilder class 的运气。
如果我正确阅读了 Oracle 文档,std 输入和输出默认定向到管道但是ProcessBuilder 有一个简单的方法供您显式set output (or input) to a file on your system or something else .
如果您希望 Python 程序使用与 Java 程序相同的输出(可能是 stdout 和 stderr),您可以像这样使用 stg:
ProcessBuilder pb = new ProcessBuilder("C:\Python\Python36-32\python.exe", "C:\test2.py");
pb.redirectOutput(Redirect.INHERIT);
Process p = pb.start();
启动 python 进程的一种方法是使用入口点 - test.cmd
echo Hello
python hello.py
这里是hello.py
#!/usr/bin/env python3
import os
if not os.path.exists('dir'):
os.makedirs('dir')
这是我的 Java 代码:
public static void main(String[] args) throws IOException {
try {
Process p = Runtime.getRuntime().exec("test.cmd");
p.waitFor();
Scanner sc = new Scanner(p.getInputStream());
while(sc.hasNextLine()){
System.out.println(sc.nextLine());
}
sc.close();
} catch (Exception err) {
err.printStackTrace();
}
}
您可以使用 ProcessBuilder API,将输出重定向到文件,然后等待结果。
public class Main {
public static final String PYTHON_PATH = "D:\Anaconda3\python.exe";
public static final String PATH_TO_SCRIPT = "D:\projects\StartScript\test.py";
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder();
builder.command(PYTHON_PATH, PATH_TO_SCRIPT);
// Redirect output to a file
builder.redirectOutput(new File("output.txt"));
builder.start().waitFor();
// Print output to console
ProcessBuilder.Redirect output = builder.redirectOutput();
File outputFile = output.file();
BufferedReader br = new BufferedReader(new FileReader(outputFile));
String st;
while ((st = br.readLine()) != null) {
System.out.println(st);
}
}
}
python 文件 test.py 包含一个简单的打印语句:
print("Hello from python")
如果不用等结果的话我想会更简单
使用过程 API 也应该有效。
就像你的例子(我使用上面声明的相同常量):
Process p = Runtime.getRuntime().exec(PYTHON_PATH + " " + PATH_TO_SCRIPT);
p.waitFor();
byte[] buffer = new byte[1024];
byte[] errBuffer = new byte[1024];
p.getInputStream().read(buffer);
p.getErrorStream().read(errBuffer);
System.out.println(new String(buffer));
System.out.println(new String(errBuffer));
要查看打印语句的输出,您需要等待并重定向流。错误流也一样。
现在,如果你像这样破坏 python 脚本:
print("Hello from python')
您应该也能看到打印的错误。