[英]Custom input to a Python program from a Java program
I am trying to make a full-fledged Java program that runs a python program.我正在尝试制作一个运行 python 程序的成熟 Java 程序。 The python program is as follows: python程序如下:
print('Enter two numbers')
a = int(input())
b = int(input())
c = a + b
print(c)
If I execute this code, the terminal looks something like this:如果我执行此代码,终端看起来像这样:
Enter two numbers
5
3
8
Now, I want the same output when executing this code from Java.现在,从 Java 执行此代码时,我想要相同的输出。 Here is my Java code:这是我的Java代码:
import java.io.*;
class RunPython {
public static void main(String[] args) throws IOException {
String program = "print('Enter two numbers')\na = int(input())\nb = int(input())\nc = a + b\nprint(a)\nprint(b)\nprint(c)";
FileWriter fileWriter = new FileWriter("testjava.py");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(program);
bufferedWriter.close();
Process process = Runtime.getRuntime().exec("python testjava.py");
InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(process.getOutputStream());
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
String output;
while (process.isAlive()) {
while (!bufferedReader.ready());
System.out.println(bufferedReader.ready());
while (!(output = bufferedReader.readLine()).isEmpty()) {
System.out.println(output);
}
bufferedReader.close();
if (process.isAlive()) {
outputStreamWriter.write(in.readLine());
}
}
}
}
But while running this program, only the first line is displayed and the first input is taken.但是在运行这个程序时,只显示第一行,并采用第一个输入。 After that, the program does not respond.之后,程序没有响应。 What mistake am I making?我犯了什么错误? And what is the solution?解决办法是什么?
Dealing with Input and Output to another process is a bit messy and you can read a good answer to how you could do it here处理另一个进程的输入和输出有点麻烦,您可以在此处阅读有关如何执行此操作的好答案
So applying those answers to your code could be something like this:因此,将这些答案应用于您的代码可能是这样的:
import java.io.*;
import java.util.Scanner;
class RunPython {
public static void main(String[] args) throws IOException {
// String program = "print('Enter two numbers')\na = int(input())\nprint(a)\nb = int(input())\nprint(b)\nc = a + b\nprint(c)";
// If you are using Java 15 or newer you can write code blocks
String program = """
print('Enter first number')
a = int(input())
print(a)
print('Enter second number')
b = int(input())
print(b)
c = a + b
print(c)
""";
FileWriter fileWriter = new FileWriter("testjava.py");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(program);
bufferedWriter.close();
Process process =
new ProcessBuilder("python", "testjava.py")
.redirectErrorStream(true)
.start();
Scanner scan = new Scanner(System.in);
BufferedReader pythonOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedWriter pythonInput = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
Thread thread = new Thread(() -> {
String input;
while (process.isAlive() && (input = scan.nextLine()) != null) {
try {
pythonInput.write(input);
pythonInput.newLine();
pythonInput.flush();
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
process.destroy();
System.out.println("Python program terminated.");
}
}
});
thread.start();
String output;
while (process.isAlive() && (output = pythonOutput.readLine()) != null) {
System.out.println(output);
}
pythonOutput.close();
pythonInput.close();
}
}
Note that the pythonInput is a BufferedWriter in Java and vice versa the pythonOutput is a BufferedReader.请注意,pythonInput 是 Java 中的 BufferedWriter,反之亦然,pythonOutput 是 BufferedReader。
Process
:使用Process
:Process p = new ProcessBuilder(
"python", "myScript.py", "firstargument Custom input to a Python program from a Java program"
).start();
You can add as many arguments as you want when running it.您可以在运行时添加任意数量的参数。
//Java code implementing Jython and calling your python_script.py
import org.python.util.PythonInterpreter;
import org.python.core.*;
public class ImportExample {
public static void main(String [] args) throws PyException
{
PythonInterpreter pi = new PythonInterpreter();
pi.execfile("path_to_script\\main.py");
pi.exec("Any custom input from this java program in python to be run");
}
}
Which embeds the Jython and can be read about here .其中嵌入了 Jython,可以在此处阅读。
Note: you may have to initialize your interpreter with your python.path
if you want to use any packages you have installed.注意:如果您想使用已安装的任何软件包,您可能必须使用python.path
初始化解释器。 You can do this by adding the following code above the code already given:您可以通过在已经给出的代码上方添加以下代码来做到这一点:
Properties properties = System.getProperties();
properties.put("python.path", ".\\src\\test\\resources"); // example of path to custom modules (you change this to where the custom modules you would want to import are)
PythonInterpreter.initialize(System.getProperties(), properties, new String[0]);
You can also return values if you need to go the other way round which can be found in the Jython documentation如果您需要反过来,您也可以返回值,这可以在Jython 文档中找到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.