代碼
Process p = Runtime.getRuntime().exec("python .\\py\\Crawler.py \"arg1\" \"arg2\" );
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line = reader.readLine()) != null){
System.out.println(line);
}
上述代碼等同于在shell中運(yùn)行命令
“python .\py\Crawler.py 'arg1' 'arg2'”
需要注意的是在Java中構(gòu)造命令是匹表,引用參數(shù)的是雙引號(hào),而非單引號(hào)峡眶。若用單引號(hào)绞呈,導(dǎo)致的問題是:python腳本獲取的參數(shù)是'arg1'而非arg1的形式惶桐,切記切記托酸!