呼叫Python代码创建简单的python声明 my_utils.py如何通过java的参数蟒蛇,从Java
def adder(a, b):
c=a+b
return c
我想在Java中值分配给蟒蛇。
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);}
}
}
但是,流程构建器无法将参数值传递给python中的a,b并显示结果。
如何设置参数值到Python?如果数值有效?怎么样,如果我通过非数字值如字符串到Python
def str(myWord):
if myWord=="OK":
print "the word is OK."
else:
print " the word is not OK."
可能的复制http://stackoverflow.com/questions/ 27235286/call-python-code-from-java-by-passing-parameters-and-results – hammerfest
你从流程中获得了输入流,而不是outp ut流是打印语句写入的地方 –