-1
            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!"<"

Tanner01
  • 1
  • 1

1 Answers1

1

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:

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:

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:
read the output from java exec
Reading InputStream from Java Process
java Process, getInputStream, read newest line only
Cannot get the getInputStream from Runtime.getRunTime.exec()
Printing a Java InputStream from a Process

dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70
  • Thank you so much for the response! Unfortunately, like I said in my post the python file is not being executed. The python script writes the data to a file after being executed, and the file is not being overwritten/updated. I think you misunderstood my question. I didn't ask how to read the output of the python file, but rather if I made a mistake calling the ProcessBuilder, and if so, how I could start to debug it. As I stated its my first time using ProcessBuilder. Thanks! – Tanner01 Aug 07 '20 at 07:03
  • If there's no exception, then python IS being executed. If the script is in the current directory, python is running it; read `p.getErrorStream()` to see any error message(s) from either python or the script. Or if you are running from a shell or 'terminal' (not an IDE like Eclipse or Intellij) use `pb.inheritIO()` or `pb.redirectError(Redirect.INHERIT)` to get the error message(s) on the terminal. Are you sure you are running in the correct current directory? Are you sure there is no output from the script to what would normally be the terminal? – dave_thompson_085 Aug 08 '20 at 20:13