2013-05-10 84 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将只包含从最后运行该命令的输出,好在这看起来像你想要做什么。