I am using Python 3.4.
I have a Python script myscript.py
:
import sys
def returnvalue(str) :
if str == "hi" :
return "yes"
else :
return "no"
print("calling python function with parameters:")
print(sys.argv[1])
str = sys.argv[1]
res = returnvalue(str)
target = open("file.txt", 'w')
target.write(res)
target.close()
I need to call this python script from the java class PythonJava.java
public class PythonJava
{
String arg1;
public void setArg1(String arg1) {
this.arg1 = arg1;
}
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
}
and I am calling runPython()
from another Java class by creating an object of PythonJava
obj.setArg1("hi");
...
obj.runPython();
I have tried many ways but none of them are properly working. I used Jython and also ProcessBuilder but the script was not write into file.txt. Can you suggest a way to properly implement this?