英文:
Running .py file in Java Eclipse
问题
try {
ProcessBuilder pb = new ProcessBuilder("C:\\Users\\--------\\PycharmProjects\\--------\\venv\\Scripts\\Python.exe", "---------.py");
Process p = pb.start();
System.out.println(p.getOutputStream());
}
catch(Exception e){
System.out.println("Exception: " + e);
}
plaintext
So I am working on a program that grabs information from Spotify's API. I have a script in python that feeds a java program the data I need. Unfortunately, I am having trouble getting eclipse to run the .py script by itself. I am using ProcessBuilder and for some reason there are no errors but yet the program isn't executing the python script. I am new to integrating multiple languages in a project so any help is appreciated! I have done hours of research trying to get this figured out. I know that there are similar posts on here regarding the same topic but none of the answers seemed to work for me. Thanks!
<details>
<summary>英文:</summary>
try {
ProcessBuilder pb = new ProcessBuilder("C:\\Users\\--------\\PycharmProjects\\--------\\venv\\Scripts\\Python.exe", "---------.py");
Process p = pb.start();
System.out.println(p.getOutputStream());
}
catch(Exception e){
System.out.println("Exception: " + e);
}
">" So I am working on a program that grabs information from Spotify's API. I have a script in python that feeds a java program the data I need. Unfortunately, I am having trouble getting eclipse to run the .py script by itself. I am using ProcessBuilder and for some reason there are no errors but yet the program isn't executing the python script. I am new to integrating multiple languages in a project so any help is appreciated! I have done hours of research trying to get this figured out. I know that there are similar posts on here regarding the same topic but none of the answers seemed to work for me. Thanks!"<"
</details>
# 答案1
**得分**: 1
这是运行脚本,只是你没有得到输出,因为你做了两件事情错误。首先,请查看 [`Process.getOutputStream`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Process.html#getOutputStream()) 的 javadoc:
> 返回与进程的正常输入连接的输出流。流的输出被导入到由此 Process 对象表示的进程的标准输入中。
这不是你想要的。要从进程中获取输出,请使用 **[`Process.getInputStream`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Process.html#getInputStream())**:
> 返回与进程的正常输出连接的输入流。该流获取从由此 Process 对象表示的进程的标准输出中导入的数据。[以及标准错误输出,如果已合并]
其次,`System.out.println(stream)`(对于输入流)不会打印可以在流上接收的数据,它只会打印流的 _对象_(作为内部类名称,atsign,哈希码)。要显示来自Python进程(即脚本)的 _数据_,您必须从流中 _读取_ 数据,然后输出已读取的数据。这方面有很多示例;我无法想象您如何在找不到至少一百个示例的情况下花费数小时。例如,尝试以下链接:
https://stackoverflow.com/questions/8149828/read-the-output-from-java-exec
https://stackoverflow.com/questions/9869101/reading-inputstream-from-java-process
https://stackoverflow.com/questions/15993591/java-process-getinputstream-read-newest-line-only
https://stackoverflow.com/questions/17038324/cannot-get-the-getinputstream-from-runtime-getruntime-exec
https://stackoverflow.com/questions/15801069/printing-a-java-inputstream-from-a-process
<details>
<summary>英文:</summary>
It _is_ running the script, you just aren't getting the output, because you did _two_ things wrong. First, see the javadoc for [`Process.getOutputStream`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Process.html#getOutputStream()):
> Returns the output stream connected to the normal input of the process. Output to the stream is piped into the standard input of the process represented by this Process object.
That's not what you want. To get the output _from_ the process **USE [`Process.getInputStream`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Process.html#getInputStream())**:
> Returns the input stream connected to the normal output of the process. The stream obtains data piped from the standard output of the process represented by this Process object. [plus stderr if merged]
Second, `System.out.println(stream)` (for an input stream) doesn't print the data that can be received on the stream, it prints only the stream _object_ (as internal classname, atsign, hashcode). To display the _data_ from the python process (i.e. the script) you must _read_ it from the stream and then output the data that was read. There are examples of this everywhere; I can't imagine how you could spend hours without finding at least a hundred. Try for example:
https://stackoverflow.com/questions/8149828/read-the-output-from-java-exec
https://stackoverflow.com/questions/9869101/reading-inputstream-from-java-process
https://stackoverflow.com/questions/15993591/java-process-getinputstream-read-newest-line-only
https://stackoverflow.com/questions/17038324/cannot-get-the-getinputstream-from-runtime-getruntime-exec
https://stackoverflow.com/questions/15801069/printing-a-java-inputstream-from-a-process
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论