2013-05-10 85 views
1

我使用的PHP這個代碼使用Lucene索引文件和搜索,但它會導致空數組...我怎樣才能得到CMD結果使用PHP

$resul = exec('set classpath=C:\lucene\lucene\core\lucene-core-4.3.0.jar;C:\lucene\lucene\queryparser\lucene-queryparser-4.3.0.jar;C:\lucene\lucene\analysis\common\lucene-analyzers-common-4.3.0.jar;C:\lucene\lucene\demo\lucene-demo-4.3.0.jar2>&1',$result); 
echo $result; 
$resul = exec('java org.apache.lucene.demo.IndexFiles -doc C:\lucene\src',$result); 
echo $result; 
$resul = exec('java org.apache.lucene.demo.SearchFiles'); 
echo $result; 

回答

1

exec每個實例都從使用一個單獨的環境所有其他人。這意味着第一個exec設置的環境變量在進行以下調用時不會「粘住」,因此類路徑很可能爲空,並且Java程序無法運行。

解決方案是將所有內容組合成一個大的命令行。在Windows中,你可以做到這一點通過&串聯的命令:

// Sorry for the unreadable line, but it has to be without linebreaks 
$commands = "set classpath=C:\lucene\lucene\core\lucene-core-4.3.0.jar;C:\lucene\lucene\queryparser\lucene-queryparser-4.3.0.jar;C:\lucene\lucene\analysis\common\lucene-analyzers-common-4.3.0.jar;C:\lucene\lucene\demo\lucene-demo-4.3.0.jar2 & java org.apache.lucene.demo.IndexFiles -doc C:\lucene\src & java org.apache.lucene.demo.SearchFiles"; 

exec($commands, $result); 

由此$result將只包含從最後運行該命令的輸出,好在這看起來像你想要做什麼。