How to pass the java parameter to python , Call Python code from Java
<p>I create simple python statement
my_utils.py</p>

<pre><code>def adder(a, b):
    c=a+b
    return c
</code></pre>

<p>I would like to assign value to python in Java.</p>

<pre><code>public class ParameterPy {
public static void main(String a[]){
try{

int number1 = 100;
int number2 = 200;

ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","D://my_utils.py",""+number1,""+number2);
Process p = pb.start();

BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));

System.out.println(".........start   process.........");  
String line = "";     
while ((line = bfr.readLine()) != null){
    System.out.println("Python Output: " + line);
}
System.out.println("........end   process.......");


}catch(Exception e){System.out.println(e);}
}
}
</code></pre>

<p>However, process builder is not able to pass the parameter value to a, b in python and display the result.
<a href="http://i.stack.imgur.com/cR99O.png" rel="nofollow"><img src="http://i.stack.imgur.com/cR99O.png" alt="enter image description here"></a></p>

<p>How to give the parameter value to python? If the numeric value is worked? How about if I pass the non-numeric value such as a string to python</p>

<pre><code>def str(myWord):
    if myWord=="OK":
        print "the word is OK."
    else:
        print " the word is not OK."
</code></pre>

<pre><code>import sys
# print sys.argv

print "sys.argv is:",sys.argv
# ['D://my_utils.py', '100', '200', 'google']

a= sys.argv[1]
b= sys.argv[2]
print "a is:", a
print "b is:", b
a= int (a)
b= int(b)

def adder(a, b):
    c=a+b
    return c

print adder(a,b)

searchTerm=sys.argv[3]
print searchTerm  ##google

def word(searchTerm):
    if searchTerm=="google":
        print " you get it"
    else:
        print " the word is different."

word(searchTerm)
</code></pre>

<p>in java </p>

<pre><code>int number1 = 100;
int number2 = 200;
String searchTerm="google";
ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","D://searchTestJava//my_utils.py",""+number1,""+number2,""+searchTerm);
Process p = pb.start();

BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));

System.out.println(".........start   process.........");  
String line = "";     
while ((line = bfr.readLine()) != null){
    System.out.println("Python Output: " + line);
</code></pre>

<p>the output result:</p>

<pre><code>Python Output: a is: 100
Python Output: b is: 200
Python Output: 300
Python Output: google
Python Output:  you get it
</code></pre>

java python parameter-passing processbuilder