Home » Php » java – How to run a shell command through PHP code?

java – How to run a shell command through PHP code?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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…

How to&Answers:

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.

Answer:

Given you are calling java. My bet is the output is being displayed in the Java Console, and not in the shell, where PHP could pull the text information.

How to solve this dilemma?

Well you could write the results to a file, if you have the java source to modify, and then read that file through php to get the results. The possibility of a collision here would be pretty good. The other option is to have Java connect to your MySQL database (if you had one) and then run the java then query the database for the response. Of course, you would need to pass Java a way for it to input the data to insert a record you could identify (a hash of some sort), I have never done that in Java, just a theory of how you might be able to do it.

Update

You may want to try the standard output as suggested by darko petreski as another option as well.

Answer:

If the PHP code is to be executed in a server (and not via command line) the user that runs the java executable is www-data, not you. In that case make sure that www-data has the permissions to read the jar file and to execute the java executable

Answer:

The first thing that I would check/change is the line in the function where you are building the $jarPath variable from this:

$jarPath = $_SERVER['DOCUMENT_ROOT'] . "myFolder/tools_new/includes/Rank.jar";

to this:

$jarPath = $_SERVER['DOCUMENT_ROOT'] . "/myFolder/tools_new/includes/Rank.jar";

The trailing slash may not be present in $_SERVER['DOCUMENT_ROOT'] which could cause issues.

I am assuming that when you say it runs from the console, you are running the java command like so:

$ java -jar /rest/of/path/myFolder/tools_new/includes/Rank.jar aspdotnet/microsoft.com

I would ensure that you include the path to the java binary in the $command variable like so…

$command = "/path/to/java -jar $jar aspdotnet/microsoft.com";

The user that owns the web server process may not have a $PATH variable that includes the path to the Java binary.