1

I am writing a PHP program and part of an XAMPP site that I have running on the localhost of my machine. I would like the file to input two of its strings into a Java program that I have saved on the desktop of my computer, and then store the output as a string. I have been trying to use shell_exec() but I have not gotten it to work.

Any tips?

    <?php
$artist = $_POST['artist'];
$songname = $_POST['songname'];

    $artist = preg_replace('/\s+/', '', $artist);
    $songname = preg_replace('/\s+/', '', $songname);

    $artist=strtolower($artist);
    $songname=strtolower($songname);

    echo $artist;
    echo $songname;

    $output = shell_exec("/Users/ianterry/Desktop/CussCalc2/java CussCalc '$artist' '$songname'");
    $output = shell_exec("/usr/bin/java -version");
    echo $output;
?>

So far, the only output I get when it is run is the two strings being echo'd.

Jabberwock Stomp
  • 313
  • 5
  • 16
  • 1
    you know $output is overriden by java version? – Robert Dec 02 '15 at 11:37
  • I did realize that. I put that in after testing the former to see if it would give any output; neither did. – Jabberwock Stomp Dec 02 '15 at 11:49
  • I dont know much PHP but looking at the docs http://php.net/manual/en/function.shell-exec.php I see that *"It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required."*. Maybe use the exec command instead and debug from there? – ug_ Dec 02 '15 at 11:50
  • maybe use proc_open instead of shell_exec at least you will see what exactly is happening – Robert Dec 02 '15 at 13:19

1 Answers1

0

At first you should check shell_exec and exec are allowed: PHP exec - check if enabled or disabled

Also, take a look at How to run Java program and get output in PHP?

Community
  • 1
  • 1
Alex Babak
  • 489
  • 3
  • 15