Cannot execute java program using php exec function

I am trying to execute a java program to sign a pdf file with the php exec function, but it does not 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 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, this works. Can anybody help me?

Thank.

+5
source share
3 answers

Finally, I was able to solve the problem.

Decision:

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

By adding a parameter -Djava.awt.headless=true, you tell java that this is an indirect call, so it does not control the keyboard, mouse, etc.

+1
source

@Treffynnon is right. The difference between executing a program from the command line and another program is the environment variables and permissions.

, , , , PHP, .

  • PROGRAM.jar
  • ORIGIN.pdf
  • DESTINY.pdf

, , .. , . , .

.

+3

Almost certainly PHP will not know the path "java". If you are on Linux, run "which java" and put the entire Java path that you will return to the exec call, for example.

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

All Articles