dqsp60748 2010-09-10 12:52
浏览 27
已采纳

如何通过PHP代码运行shell命令?

I am trying to run a Jar file in the backend of my php code.But I am not getting the desired output to it.There is a jar file which runs in the background and returns the Page Rank of any of the keyword and Domain given to it. I am attaching the code,please suggest me any solution to it,because when I run it on the terminal,it is giving correct output.

Here is the Code :

    <?php
set_time_limit(0);
function returnJarPath()
{
    $jarPath = $_SERVER['DOCUMENT_ROOT'] . "myFolder/tools_new/includes/Rank.jar";
    return $jarPath;
}
$jar = returnJarPath();
$command = "java -jar $jar aspdotnet/microsoft.com";//Passing the Argument to the Jar file.


$shellOutput = shell_exec($command);
    print "The Shell Output is : " ; var_dump($shellOutput);print "<br />";
exec($command,$executeCommmand);
    print "The Exec returns the value : " ; var_dump($executeCommmand);print "<br />";
passthru($command,$passthruCommand);
    print "The Passthru returns the value : " . $passthruCommand. "<br />";
?>

I just checked apache's error log and the last error I found was :

sh: java: command not found

But as I have already said,I have been using the same command through SSH to run the Java command.So there's no such possibility of not having JAVA installed on the server.Please help me out of this mess...

  • 写回答

4条回答 默认 最新

  • doushun9875 2010-09-10 13:03
    关注

    If the jar file writes to standard output you can use exec.

    Here is an example how I use it:

    may be first: exec("cd jar dir"); // if jar fine needs to be executed from the same dir
    $output = exec("/usr/bin/java -jar $jar aspdotnet/microsoft.com");
    

    But as you say:

    sh: java: command not found
    

    It means the there is no path alias to java from php. Just use the full java path to the executable /usr/bin/java.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: