0

I am trying to execute a .jar file from a PHP script using the exec() command, more exactly using:

<?php

    $command = 'java -jar myfile.jar';

    exec( $command, $output);

?>

both the php script, and the jar are in the same directory on the server. The myfile.jar produces an excel spreadsheet, and I want this spreadsheet to be created or stored in the same directory or another directory for that matter on the server. When I execute myfile.jar in the terminal of my pc, the program works fine and outputs the file to the directory where the .jar is located.

Any ideas?

Best Regards.

Aerials
  • 4,231
  • 1
  • 16
  • 20
  • 1
    Most likely this is a permissions issue where the server (Apache?) isn't actually allowed to run java, or the java script doesn't have access write access to be able to write the file. Try running the php script yourself via the cli, by typing `php -q ` and see what happens. If it works, that means you have a very high probability that is indeed is a permissions problem. – Tularis Jul 07 '16 at 09:23
  • 1
    You can try `$command = 'cd output_folder && java -jar /full/path/to/myfile.jar';` – Arnauld Jul 07 '16 at 09:29
  • 1
    1) What does **var_dump($output);** say? If it tells u nothing, 2) create a shell script with the java call in it and make the stderr (use 2> ) output to a file. Call the script from php, then inspect possible stderr errors in the file. 3) Make sure java is callable from the directory you're working from for the user you're working with. – Werner Jul 07 '16 at 09:36

2 Answers2

0

If I understand correctly, you want to execute the myfile.jar file, which returns some data and save it to a file? If you don't need to edit the data, you can use:

exec ( 'java -jar myfile.jar > output.xml' );
Scriptman
  • 424
  • 2
  • 13
  • This worked, and I could read from the output.xml that it was being truncated by Fatal Error.Now the java program finishes its run, and no problem there. However, the excel file is being created and comes out empty. I suspect this has something to do with the FileOutputStream that the java code uses to generate the spreadsheet. Can you point me in the right direction ? – Aerials Jul 07 '16 at 15:25
  • Hello Aerials, I found here something about your issue: http://stackoverflow.com/questions/3789691/how-to-run-java-program-and-get-output-in-php – Scriptman Jul 08 '16 at 07:17
0

Ok, so I installed PHP on my windows, and am using EasyPHP to test as if I had the server on my own computer. The php code is working fine, but from the $output variable I could notice that the program suddenly gave a Fatal Error. I managed to dechipher that this error was from the time limit PHP has for running scripts. I used set_time_limit(0); and corrected this. Now the java program finishes its run, and no problem there. However, the excel file is being created and comes out empty. I suspect this has something to do with the FileOutputStream that the java code uses to generate the spreadsheet. Can you point me in the right direction ?

Aerials
  • 4,231
  • 1
  • 16
  • 20