5

I'm trying to execute a java program to sign a pdf file with php exec function but 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, the $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

In command line it works. Can anyone help me?

Thank you.

3
  • 3
    Does it work if you supply the full path to the java binary? For example /usr/bin/java. Use which java to find the path to the binary. Commented Feb 27, 2012 at 15:38
  • 1
    In addition to @Treffynoon 's comment, you may also need to specify the full path to your PROGRAM.jar, ORIGIN.pdf and DESTINY.pdf files Commented Feb 27, 2012 at 15:48
  • I just use full path for the files and command and doesn't work. Commented Feb 27, 2012 at 17:01

3 Answers 3

3

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

So, first check whether user that runs your server where PHP is running has permissions to run the application and to access appropriate files.

Then verify the path to

  1. java
  2. PROGRAM.jar
  3. ORIGIN.pdf
  4. DESTINY.pdf

You should probably modify the path, i.e. better specify it either using relative or absolute notation. It is because current working directory might be different in 2 cases.

Good luck.

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

Comments

3

Almost certainly PHP won't know the path of "java". If you're in Linux, run "which java" and put the whole java path that you get back in the exec call, e.g.

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

Comments

1

Finally I could resolve the problem.

The solution is:

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

Adding the -Djava.awt.headless=true option you're telling java that it's an indirect call so it hasn't control over keyboard, mouse, etc.

1 Comment

hi this is not working for me, i tried the above code, i am still getting an empty array for result

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.