Search code examples
javaphpexec

Empty Array on running Java on PHP


I have a java program in a .jar file, which I need to run through PHP. I have to send in 3 arguments (all 3 of them are strings, the first being a pretty big one), and receive an output after the execution.

I am using XAMPP and Symfony on Windows 10 for the whole project, although I doubt it's integral to the problem.

I have tried a few possibilities

At first I've tried:

exec("java -jar 'D:\Path\To\File\src\Service\Java\JavaFile.jar' ". $argument1 ." ". $argument2 ." ". $argument3. " 'package.Main'", $output, $errorcode)

Which didn't give me any result after var_dump/print_r/echo the $output

After that I went through a bunch of options (all listed below) while still not getting any output - mostly I received an empty array.

exec("java -jar 'D:\Path\To\File\src\Service\Java\JavaFile.jar' \"". $argument1 ."\" \"". $argument2 ."\" \"". $argument3. "\" 'package.Main'  2>&1", $output, $errorcode)
exec("java -jar 'D:\Path\To\File\src\Service\Java\JavaFile.jar' '". $argument1 ."' '". $argument2 ."' '". $argument3. "' 'package.Main' 2>&1", $output, $errorcode)
$output = shell_exec("java -jar 'D:\Path\To\File\src\Service\Java\JavaFile.jar' '". $argument1 ."' '". $argument2 ."' '". $argument3. "' 'package.Main' 2>&1")

This one however gave me a different output:

I simply didn't send the arguments

$output = shell_exec("java -jar 'D:\Path\To\File\src\Service\Java\JavaFile.jar' 'package.Main' 2>&1")

Unable to access jarfile

Nothing I've found on the internet had helped. Answering some questions that might appear at first:

  • yes, the jar does run properly in CLI
  • yes, I can run basic commands through exec() and shell_exec() receiving an output
  • yes, I did restart the server multiple times and it had no effect on the result
  • yes, the .jar file is there
  • the .jar file NEVER gives empty output based on the code

I am hoping for some help as this is a really important part for the project.


Solution

  • After over a week of fighting with this, I've managed to fix my problem.

    This might not be avaliable solution for you.

    My service will be running on Linux Server so this solution was avaliable for me.


    I've gotten in contact with a colleague of mine who is far more experienced with PHP than me. It took us about a week to come with conclusion that I need to change the environment from Windows to Linux.

    To avoid installing a new OS on my device, the colleague proposed using Docker. After few hours of work and following a two part tutorial (part 1 part 2) on how to setup PHP envoirnment in Docker (with Symfony, Mysql and Nginx) I managed to make it work.

    I installed Java inside php container (an important part that caused me lots of confusion and problems), made sure that the paths to all the files are correct and that the .jar file has all the permissions. After that the exec function worked swimingly giving me the expected output.

    Additionally, I've saved the first argument (which was a really long string) into a file and I've passed the path to the file as the argument instead. Mind that I did that before I switched to Docker, so it might've worked with big strings after all.


    TLDR;

    • switch to Linux (fe. Debian) - optionally using VM if you have to (fe. Docker)
    • install Java next to PHP so they can communicate
    • make sure all the paths are valid
    • make sure the .jar file has all the required permissions (chmod)