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>

<p>The Python <code>sys</code> module provides access to any command-line arguments via the <code>sys.argv</code>. But the type of arguments is string always. Here is the example how I would like to check numeric values:</p>

<pre><code>import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

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

def is_number(x):
    try:
        float(x)
        return True
    except ValueError:
        return False

p1 = sys.argv[1]
p2 = sys.argv[2]

if is_number(p1) and is_number(p2):
    print "Sum: {}".format(adder(float(p1), float(p2)))
else:
    print "Concatenation: {}".format(adder(p1, p2))
</code></pre>

<p>UPDATED: As @cricket_007 mentioned, you can use <code>isdigit</code> if you want to check integers only. But it does not work for <code>float</code>:</p>

<pre><code>&gt;&gt;&gt; "123".isdigit()
True
&gt;&gt;&gt; "12.3".isdigit()
False
</code></pre>

java python parameter-passing processbuilder