I don't think reading the executable (a series of binary numbers) will do any good. Also, The class java.lang.ProcessBuilder, in Java 1.5, is used to create operating system processe. Instead you should use the jython API: http://www.jython.org/javadoc/org/python/util/PythonInterpreter.html

Particularly: PyInstance and PythonInterpreter;

Answer from mattsap on Stack Overflow
🌐
Mkyong
mkyong.com › home › java › java processbuilder examples
Java ProcessBuilder examples - Mkyong.com
January 19, 2019 - cuz i am using itellij and i am facing problems with python inside intellij. ... What if I use waitFor(timeout, timeUnit) instead of waitFor()? Should I also call Process.destroy() after waitFor(timeout, timeUnit)? ... If you are using single thread process.waitFor()? current Thread block unit Destroy the process if you are using multiThread when process.destroy() process immediately destroy i hope this help your doubt ... could you please let me know if there is an existing OpenSource Java library which creates OS based tasks (task scheduler on windows, crontab on linux), reads the task execution history ?
🌐
Wishusucess
wishusucess.com › home › blog › run python script in java using processbuilder
Python Script in Java Code Using ProcessBuilder - Wishusucess
June 7, 2020 - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ReadPython { public static void main(String[] args) throws IOException, InterruptedException { String path = "C:\Users\Admin\Desktop\pythoncodetest/script.py"; ProcessBuilder pb = new ProcessBuilder("python","C:\Users\Admin\Desktop\pythoncodetest/script.py").inheritIO(); Process p = pb.start(); p.waitFor(); BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = bfr.readLine()) != null) { System.out.println(line); } } }
Discussions

calling python method through java ProcessBuilder() - Stack Overflow
How can I call a method in a Python file using ProcessBuilder in java? My Python file has a class and three methods. Also, how can I set my python method params from ProcessBuilder? Here is my Jav... More on stackoverflow.com
🌐 stackoverflow.com
Running python from Java using Process Builder - Stack Overflow
For an existing Java code that I want to extend, I need to run a python code from within Java. I am using Process builder for this: ProcessBuilder pb = new ProcessBuilder("python", "/directoryp... More on stackoverflow.com
🌐 stackoverflow.com
April 1, 2016
Java ProcessBuilder not able to run Python script in Java - Stack Overflow
My recommendation was only to see ... stream immediately after creating your ProcessBuilder object, you're following my recommendations. Then something else might be wrong. How do you run your python script if you weren't using Java?... More on stackoverflow.com
🌐 stackoverflow.com
Running a python code using process builder Java - Stack Overflow
I need some help. I am trying to run a python script called mantime.py from a directory. I've tried to google it and found several ways to do it. Yet, I still got 2 as the exit value, which I expec... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › ProcessBuilder.html
ProcessBuilder (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... This class is used to create operating system processes. Each ProcessBuilder instance manages a collection of process attributes. The start() method creates a new Process instance with those attributes. The start() method can be invoked repeatedly from the ...
🌐
Delft Stack
delftstack.com › home › howto › java › call python script from java code
How to Call Python Script From Java Code | Delft Stack
February 2, 2024 - Our Python script is: print("Hello, This is Delftstack.com! The Best Tutorial Site!") ... import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Example { public static void main(String[] args) throws IOException, InterruptedException { String Script_Path = "C:\\Users\\Sheeraz\\script.py"; ProcessBuilder Process_Builder = new ProcessBuilder("python", Script_Path).inheritIO(); Process Demo_Process = Process_Builder.start(); Demo_Process.waitFor(); BufferedReader Buffered_Reader = new BufferedReader(new InputStreamReader(Demo_Process.getInputStream())); String Output_line = ""; while ((Output_line = Buffered_Reader.readLine()) != null) { System.out.println(Output_line); } } }
🌐
Helicaltech
helicaltech.com › blog › ways to execute python code from java
Ways to Execute Python Code From Java - Helical IT Solutions Pvt Ltd
Big Data Consulting Services, Big Data Analytics - Helical IT solutions Pvt Ltd
There are many ways to execute Python code from with in Java. In case if your project has requirement to execute Python code from Java, here are few code samples that I have collected from Internet. First way is using Jython: 3. Invoking native python interpreter using Java Helical IT Solutions Pvt Ltd offers Jaspersoft consulting, Pentaho consulting, Talend consulting & big data consulting services. Helical IT Solutions Pvt Ltd, based out of Hyderabad India, is an IT company specializing in Data Warehousing, Business Intelligence and Big Data Analytics Services.
Rating: 4.4 ​
🌐
ZetCode
zetcode.com › java › processbuilder
Java ProcessBuilder - creating processes in Java
May 1, 2025 - Learn how to create and manage operating system processes in Java using ProcessBuilder. This tutorial covers executing commands, handling process input/output, and optimizing system interactions in Java applications.
🌐
Stack Overflow
stackoverflow.com › questions › 35685056 › running-python-from-java-using-process-builder
Running python from Java using Process Builder - Stack Overflow
April 1, 2016 - For an existing Java code that I want to extend, I need to run a python code from within Java. I am using Process builder for this: ProcessBuilder pb = new ProcessBuilder("python", "/directoryp...
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › how to call python from java
How to Call Python From Java | Baeldung
August 27, 2025 - Throughout this tutorial, we’ll use a very simple Python script which we’ll define in a dedicated file called hello.py: ... Assuming we have a working Python installation when we run our script, we should see the message printed: ... In this section, we’ll take a look at two different options we can use to invoke our Python script using core Java. Let’s first take a look at how we can use the ProcessBuilder API to create a native operating system process to launch python and execute our simple script:
🌐
Quora
quora.com › How-do-I-call-python-function-using-process-builder
How to call python function using process builder - Quora
Answer (1 of 2): [code]Process p = new ProcessBuilder("{{ python-command }}", "{{ arguments }}").start(); [/code]Your python file, file.py: [code]def foo(): //your prof def main(): if argv[0]=="foo": foo() if __name__ == "__main__": main() [/code]{{ python-command }} will be...
🌐
Medium
medium.com › @chamlinid › integrate-java-and-python-code-bases-1c4819fe19da
Integrate Java and Python code bases | by Chamlini Dayathilake | Medium
February 14, 2025 - Write a function inside a Python script and invoke the function inside the script itself. Use print(), to return values from the script. Create a ProcessBuilder instance in the Java code, with the path of the script file and required parameter ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-processbuilder-class-java
Java.lang.ProcessBuilder class in Java - GeeksforGeeks
January 14, 2022 - ProcessBuilder(String... command): This constructs a process builder with the specified operating system program and arguments.
Top answer
1 of 5
3

Usually when executing commands using ProcessBuilder, PATH variable is not taken into consideration. Your python C:/Machine_Learning/Text_Analysis/Ontology_based.py is directly working in your CMD shell because it can locate the python executable using the PATH variable. Please provide the absolute path to python command in your Java code. In below code replace <Absolute Path to Python> with the path to python command and its libraries. Usually it will something like C:\Python27\python in Windows by default

package text_clustering;

import java.io.*;

public class Similarity {

    /**
     * 
     * @param args
     * 
     */
    public static void main(String[] args){
        try{
            String pythonPath = "C:/Machine_Learning/Text_Analysis/Ontology_based.py";
            //String pythonExe = "C:/Users/AppData/Local/Continuum/Anaconda/python.exe";
            ProcessBuilder pb = new ProcessBuilder(Arrays.asList("<Absolute Path to Python>/python", pythonPath));
            Process p = pb.start();

            BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";
            System.out.println("Running Python starts: " + line);
            int exitCode = p.waitFor();
            System.out.println("Exit Code : "+exitCode);
            line = bfr.readLine();
            System.out.println("First Line: " + line);
            while ((line = bfr.readLine()) != null){
                System.out.println("Python Output: " + line);


            }

        }catch(Exception e){System.out.println(e);}
    }

}
2 of 5
0

Reading from stdin returns null when the script is killed/dies. Do a Process#waitFor and see what the exitValue is. If it isn't 0 then it's highly probable that your script is dying.

I'd try making it work with a dumb script that only writes a value. Make sure that you print all error information from python.

🌐
Stack Overflow
stackoverflow.com › questions › 31225697 › running-a-python-code-using-process-builder-java
Running a python code using process builder Java - Stack Overflow
I am trying to run a python script called mantime.py from a directory. I've tried to google it and found several ways to do it. Yet, I still got 2 as the exit value, which I expect it 0 (terminate normally). Here is my code: Copypublic int performedManTime() throws IOException, InterruptedException{ ProcessBuilder pb = new ProcessBuilder("/usr/bin/python","/Users/ab/Downloads/ManTIME/mantime.py","-ppp","test",inputDir.getAbsolutePath(),"i2b2"); Map<String,String>env = pb.environment(); env.put("MANTIME_CRF_TRAIN", "/usr/local/Cellar/crf++/0.58/bin/crf_learn"); env.put("MANTIME_CRF_TEST", "/usr
🌐
Tutorialspoint
tutorialspoint.com › java › lang › java_lang_processbuilder.htm
Java ProcessBuilder Class
Python TechnologiesDatabasesComputer ... QualityManagement Tutorials View All Categories ... The Java ProcessBuilder class is used to create operating system processes.This class is not synchronized....
🌐
Javatpoint
javatpoint.com › processbuilder-in-java
ProcessBuilder in Java - Javatpoint
ProcessBuilder in Java with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Reddit
reddit.com › r/javahelp › how to start a python process from java and do io communication?
r/javahelp on Reddit: How to start a python process from Java and do IO communication?
August 11, 2022 -

I'm a python dev, like I know literally no Java. But because I'm experienced in python I can at least kinda understand what I'm doing in Java. But I'm stuck on this. I know it's basic shit, I can do this in python, but I just can't read the language so I can't read any help or guides either.

All I need to do really is start a python program from Java as a subprocess, receive it's output stream and be able to send to its input stream.

The python program is a discord bot using async/await stuff and will run continuously so it needs to be uninterrupted, like if it was run from the terminal EXCEPT the Java program can send input and receive output.

I also suck att processes and stuff (this is on Windows btw) and as I said I suck at Java too so sorry if I gave to little information or something. Just tell me if that's the case.

Thanks in advance :D

Top answer
1 of 5
3
just use some sort of medium where python or java reads/writes to say a .json file. To run things on your machine using java you can use https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String) Runtime.exec(String); where the String is some system dependent command (like a command line for windows, if you can start a python script from cmd you can use Runtime.exec to run the script) You can get the Process object https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html from that Runtime.exec call it returns a Process object. I've not gone this deep into the Process class but according to the docs: By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock. Where desired, subprocess I/O can also be redirected using methods of the ProcessBuilder class.
2 of 5
3
You can spawn a thread to consume the output stream of the process and similarly you can provide a thread to which you can supply input to feed to the process. I'd consider using ProcessBuilder to make it tidier, than Runtime.exec, to work with any arguments, redirection and environment - otherwise you're manipulating the commandline directly (with all of the escaping considerations). Starting a Process with ProcessBuilder will then let you access the IO and pass them to producer / consumer threads as appropriate.
🌐
Javatpoint
javatpoint.com › java-processbuilder-example
Java ProcessBuilder Example - Javatpoint
Java ProcessBuilder Example with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Stack Overflow
stackoverflow.com › questions › 61595186 › how-to-use-java-processbuilder-to-execute-a-class-from-another-class
How to use Java Processbuilder to execute a class from another class - Stack Overflow
Now, when you run your Main.java, you'll be able to see what output/errors your subprocesses are printing. If you see the errors below: Error: Could not find or load main class praktikum.Client Error: Could not find or load main class praktikum.Server · as a workaround, I'd advice to pass the absolute path to the ProcessBuilder instead of '.' especially if you are running from an IDE: