java调用python程序打印helloworld
时间: 2023-06-05 13:07:01 浏览: 104
您好!关于您的问题,我可以告诉您如何在Java中调用Python程序并打印"Hello, world!"的结果。首先,您需要在Java中使用 Runtime 类调用 Python 程序,并使用 Process 类来处理输出结果。以下是一个示例代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class PythonHelloWorld {
public static void main(String[] args) throws IOException {
String command = "python -c \"print('Hello, world!')\"";
Process process = Runtime.getRuntime().exec(command);
InputStream input = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
```
这段代码通过调用 Python 命令在控制台中打印出 "Hello, world!",并使用 BufferedReader 类来读取 Python 程序的输出。希望这可以帮到您!
阅读全文
相关推荐

















