1

I tried to execute a jar file using php's shell_exec(). The pgp file runs on a server.

I researched the problem and tried the following things but no success so far:

  1. I specify the full java path in the command
  2. I make sure shell_exec()/exec() function is not disabled on the server, the server is not run on safe mode
  3. I change the permission of the jar file to be executable
  4. I also try using exec()
  5. I test command like exec("ls", $out), and it works fine

The command looks like:

shell_exec("/usr/lib/jvm/java-7-oracle/bin/java -jar PATH_TO_MY_JAR_FILE.jar");

I copy and paste the command into shell and it runs fine.

----------------------------Update-------------------------------------

I made some progress on debugging and want to update the problem a little bit.

I tried to debug using

exec("command from above", $output, $exit_code)
echo $output;
echo $exit_code;

and done some fixing and now I am sure the php script is executed but it's not doing what I want.

So the jar file's purpose is to create a json file in the system. I specify the absolute path for this json.

Now, after I run the php file, the $output shows

Array ( [0] => {"balance":"c","num":"b","is_vip":true,"name":"a"} ) 

This is the same output when I run the command in shell

$exit_code's value is 10

But I can't find the json file in the system.

1 Answer 1

1

Did you check if the php safe mode is enabled on the server? The documentation of http://php.net/manual/en/function.shell-exec.php says:

Note: This function is disabled when PHP is running in safe mode.

Nevertheless: the documentation also says, that the safe_mode is deprecated since PHP5.3 so hopefully, this does not affect your server.

Additionally you should check if the php user is allowed to execute +x the java binary.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! just checked, the safe mode is off, I also changed the permission of the jar file to be executable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.