Java – Java programs cannot be executed using the php exec function

Java programs cannot be executed using the php exec function… here is a solution to the problem.

Java programs cannot be executed using the php exec function

I’m trying to execute a java program to sign a pdf file using the php exec function, but it doesn’t work :

exec('java -jar PROGRAM.jar -n -t ORIGIN.pdf -o DESTINY.pdf -s CERTIFICATE -p PASSWORD', $output, $return);

When I execute it, $output is an empty array and $return is an int(1), but if I run:

java -jar PROGRAM.jar -n -t ORIGIN.pdf -o DESTINY.pdf -s CERTIFICATE -p PASSWORD

On the command line, it works. Can anyone help me?

Thank you.

Solution

@Treffynnon is right. The difference between executing programs from the command prompt and other programs is environment variables and permissions.

Therefore, first check that the user running on the server running PHP has permission to run the application and access the appropriate files.

Then verify the path

  1. Java
  2. Program .jar
  3. Source.pdf
  4. Fate .pdf

You should probably modify the path, i.e. it is best to specify it using relative or absolute notation. This is because the current working directory may be different in two cases.

Good luck.

Related Problems and Solutions