Have you looked at these? They suggest different ways of doing this:

Call Python code from Java by passing parameters and results

How to call a python method from a java class?

In short one solution could be:

public void runPython() 
{ //need to call myscript.py and also pass arg1 as its arguments.
  //and also myscript.py path is in C:\Demo\myscript.py

    String[] cmd = {
      "python",
      "C:/Demo/myscript.py",
      this.arg1,
    };
    Runtime.getRuntime().exec(cmd);
}

edit: just make sure you change the variable name from str to something else, as noted by cdarke

Your python code (change str to something else, e.g. arg and specify a path for file):

def returnvalue(arg) :
    if arg == "hi" :
        return "yes"
    return "no"
print("calling python function with parameters:")
print(sys.argv[1])
arg = sys.argv[1]
res = returnvalue(arg)
print(res)
with open("C:/path/to/where/you/want/file.txt", 'w') as target:  # specify path or else it will be created where you run your java code
    target.write(res)
Answer from mkaran on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › how to call python from java
How to Call Python From Java | Baeldung
August 27, 2025 - In this first example, we’re running the python command with one argument which is the absolute path to our hello.py script. We can find it in our test/resources folder. To summarize, we create our ProcessBuilder object by passing the command and argument values to the constructor. It’s also important to mention the call to redirectErrorStream(true).
🌐
Stack Overflow
stackoverflow.com › questions › 37028938 › call-python-script-with-arguments-from-java
Call python script with arguments from java - Stack Overflow
// rest of your code ScriptEngine engine = manager.getEngineByName("python"); engine.put(ScriptEngine.ARGV, array_of_strings); engine.eval(new FileReader("test.py"), context); // rest of your code · Where array_of_strings is an array of strings to be used as the arguments.
🌐
pytz
pythonhosted.org › javabridge › java2python.html
Calling Python from Java — python-javabridge 1.0.12 documentation
import javabridge cpython = javabridge.JClassWrapper('org.cellprofiler.javabridge.CPython')() d = javabridge.JClassWrapper('java.util.Hashtable')() result = javabridge.JClassWrapper('java.util.ArrayList')() d.put("result", result) cpython.execute( 'import javabridge\n' 'x = { "foo":"bar"}\n' 'ref_id = javabridge.create_and_lock_jref(x)\n' 'javabridge.JWrapper(result).add(ref_id)', d, d) cpython.execute( 'import javabridge\n' 'ref_id = javabridge.to_string(javabridge.JWrapper(result).get(0))\n' 'assert javabridge.redeem_jref(ref_id)["foo"] == "bar"\n' 'javabridge.unlock_jref(ref_id)', d, d)
🌐
my tiny TechBlog
norwied.wordpress.com › 2012 › 07 › 23 › pass-arguments-from-java-to-python-app
pass arguments from Java to Python app – my tiny TechBlog
June 1, 2018 - In the following for loop we copy all arguments from args to cmd to have them passed in rt.exec(cmd). Of course if you have security concerns you should perform checks on the user input before passing it to your script, but that’s another story. The rests stays the same, so read the response of python using a BufferedReader. The output generated by the script is then read line by line with a BufferedReader and displayed in the terminal where we execute the java application.
🌐
YouTube
youtube.com › watch
Execute a python script with few arguments in java | Pass Arguments to Python script using java - YouTube
In this video we learn Execute a python file with few arguments in java , pass arguments using ProcessBuilder class, using process builder class we can execu...
Published   June 5, 2021
🌐
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 ​
🌐
Quora
quora.com › How-do-I-call-Python-script-from-Java
How to call Python script from Java - Quora
Answer (1 of 4): The old approach. [code]Process p = Runtime.getRuntime().exec("python yourapp.py"); [/code]You can read up on how to actually read the output here: http://www.devdaily.com/java/edu/pj/pj010016 There is also an Apache library (the Apache exec project) that can help you with thi...
Find elsewhere
Top answer
1 of 4
2
You can use Java Runtime.exec() to run python script, As an example first create a python script file using shebang and then set it executable.#!/usr/bin/python import sys print ('Number of Arguments:', len(sys.argv), 'arguments.') print ('Argument List:', str(sys.argv)) print('This is Python Code') print('Executing Python') print('From Java')if you save the above file as script_python and then set the execution permissions usingchmod 777 script_pythonThen you can call this script from Java Runtime.exec() like belowimport java.io.*; import java.nio.charset.StandardCharsets; public class ScriptPython { Process mProcess; public void runScript(){ Process process; try{ process = Runtime.getRuntime().exec(new String[]{"script_python","arg1","arg2"}); mProcess = process; }catch(Exception e) { System.out.println("Exception Raised" + e.toString()); } InputStream stdout = mProcess.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stdout,StandardCharsets.UTF_8)); String line; try{ while((line = reader.readLine()) != null){ System.out.println("stdout: "+ line); } }catch(IOException e){ System.out.println("Exception in reading output"+ e.toString()); } } } class Solution { public static void main(String[] args){ ScriptPython scriptPython = new ScriptPython(); scriptPython.runScript(); } }Hope this helps and if not then its recommended to join our Java training class and learn about Java in detail.
2 of 4
0
There are three ways to get this done:runtime approachprocess approachjython approachHave a look at this blog for detailed explanation with example.
🌐
Alibaba Cloud
topic.alibabacloud.com › a › to-invoke-a-python-script-in-java-with-dynamic-arguments_1_29_30248189.html
To invoke a Python script in Java with dynamic arguments
June 12, 2018 - 2. So after giving up Jython, I used the command line from Java to execute the PY runtime.getruntime (). exec (args), and then use the output stream to get the parameters. In the case of the packaged py script, if you do not need to pass in parameters and only need to execute the PY file, then ...
🌐
Stack Overflow
stackoverflow.com › questions › 16469232 › executing-python-script-from-java-passing-arguments
Executing python script from java passing arguments - Stack Overflow
May 18, 2017 - Call python script within java code (runtime.exec) 0 · How to execute a python script from Java? 12 · How to execute Python script from Java (via command line)? 1 · Passing arguments to Python script in Java · 0 · How to Run a Python Program that Takes Command-Line arguments from within a Java Program ·
🌐
Blogger
learn-selenium-automation-testing.blogspot.com › 2021 › 06 › how-to-call-a-python-script-with-arguments-from-java-class.html
Learn Selenium Automation Testing: How to Call a Python Script with Arguments from Java class
In this post you will learn How to Call a Python Script with Arguments from Java class , pass arguments using ProcessBuilder class, using process builder class we can execute python file and provide multiple arguments in string format.
🌐
Codingdeeply
codingdeeply.com › home › call python script from java: seamless integration guide
Call Python Script from Java: Seamless Integration Guide
February 23, 2024 - Several methods and techniques are available for calling Python code from Java, depending on your specific requirements. One common approach is to use the ProcessBuilder class to call the Python interpreter and pass the script name as an argument...
🌐
The SW Developer
theswdeveloper.com › post › java-python-jep
How to execute Python code in Java - The SW Developer
September 10, 2021 - Inject a String argument which ... script argument named user_name will get the value of our Java code name variable. ... That's it. It's as simple as that. Now you can install any Python library you need, import it in your Python code and then ...
🌐
my tiny TechBlog
norwied.wordpress.com › 2012 › 03 › 28 › call-python-script-from-java-app
call python script from Java app – my tiny TechBlog
June 1, 2018 - Basically we execute the python script by envoking python2.6 and provide the path to the script as parameter, as you would normally do it if you exec the script in a terminal/bash.