如何通过PHP代码运行shell命令? [英] How to run a shell command through PHP code?

查看:98
本文介绍了如何通过PHP代码运行shell命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的php代码的后端运行一个Jar文件。但是我没有得到它所需的输出。有一个jar文件在后台运行并返回任何关键字的页面排名和域名。
我附上代码,请告诉我任何解决方案,因为当我在终端上运行时,它会提供正确的输出。

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.

这是代码:

    <?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 />";
?>

我刚检查了apache的错误日志,我发现的最后一个错误是:

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


sh:java:command not found

sh: java: command not found

但是我已经说,我一直在通过SSH使用相同的命令来运行Java命令。所以没有可能没有在服务器上安装JAVA。请帮我摆脱这个烂摊子......

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...

推荐答案

如果jar文件写入标准输出,则可以使用exec。

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");

但正如你所说:

sh: java: command not found

这意味着没有来自php的java的路径别名。只需使用可执行文件/ usr / bin / java的完整java路径。

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

这篇关于如何通过PHP代码运行shell命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆