1

After reading some of the posts in this website related to exactly the same issue I've got, I found that none of them were giving me a successful result:

How to run a jar from a web server using PHP

Run Java class file from PHP script on a website

why exec('java -jar file.jar') dont work on browser but works on command line?

I'm using a Windows Server 2008 R2 datacenter, 64 bits, it's an EC2 instance from AWS. The command that I use to run my jar in a php file is exec, which works for any phyton script and normal .exe programs. But when it comes to a jar, it does nothing.

This is my code (my jar needs 5 parameters):

$script = "java -jar scripts/IDW.jar 5 none 2 values_grid values";      
$result = exec($script, $output);
if ($result) {
    print_r($output);           
}
else {
    echo "Failed";
}
echo "<br/>Result: ". $result;

The result I have is:

Failed
Result:

When I run the jar on console, it runs perfectly, but not from PHP, which means I am passing the right parameter values at the above Php code.

Java version: 1.7.0_51 and PHP version: 5.4.26

I would highly appreciate any comments on this.

Community
  • 1
  • 1
user3927433
  • 13
  • 1
  • 3
  • Try to adjust the path to `java`, e.g `whereis java`. – Marco Aug 10 '14 at 17:43
  • And what is inside `$output`? – Tyralcori Aug 10 '14 at 17:44
  • @d3l The path is adjusted already to java. The final part of it is: 'C:\Program Files\Java\jre7\bin;'. Also my 'Java environment variable' is set to 'C:\Program Files\Java\jre7'; – user3927433 Aug 11 '14 at 21:32
  • @Tyralcori I added a new line to show the $output and it is: 1, although my jar file should return a different output, actually, it should be many lines... – user3927433 Aug 11 '14 at 21:39

3 Answers3

1

You can check with shell_exec, system() and other function pctl function as well. These functions might be disabled, so before executing check them or you can use this as well

function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
} 

More detail found here.

Hrishikesh Mishra
  • 3,295
  • 3
  • 27
  • 33
  • Hi @Hrishikesh, I tried your code and it seems unstoppable. It never comes back. It's always waiting and I think it gets stuck. – user3927433 Aug 11 '14 at 21:42
0

You have to do something like this:

//REPLACE THIS PATH WITH THE REAL JAVA PATH
$handle = popen(`"C:\\Program Files (x86)\\Java\\jre7\\bin\\java.exe arguments"`, "r"));
//You can read with $read = fread($handle, 2096)
pclose($handle);

exec will not work on Windows (for "external" executables) and in most cases you'll have to provide the full path to Java.

0

This is not a straight answer, but if calling java from Php does not work under any circumstances, you can also run jar files from Python scripts. I mean Php --> Python --> jar.

Calling Python scripts from Php always works for me.

This post helped me in the past:

Python: How can I execute a jar file through a python script

Community
  • 1
  • 1