0

I want to call a java class file from PHP, and display the text output (indicating progress) in real time to the user's browser. The passthru(), echo() and system() PHP functions won't work, and a php/java bridge seems like overkill. The code below (very simplified but self contained) demonstrates how the passthru() function, which ostensibly returns the java output stream in real time, doesn't work in this case. In the code below, the text stream only appears in the browser once the call to java has been completed, ie no real time progress updates.

I have tried this solution, and others like it suggested on Stack Overflow, to no avail.

Grab results from a php exec() while the command is still running?

What would you suggest?

index.html:

<html lang="en">    
 <head>
 <meta charset="utf-8" />       
 <script src="jquery.js"></script>                      
 <script type="text/javascript">        
  $(document).on("click","#javacode_button",function(){
$("#java_output").load("innerpage.php",function(responseTxt,statusTxt,xhr){
});     
  });
  </script>     
  </head>
  <body>
    <div id="java_output"></div>
    <input type="Button" id="javacode_button" value="Run Java Code"/>
  </body>
</html>

innerpage.php:

<?php 
   echo 'start process <br>';
   echo passthru('java timer ');    
   echo '<br> end process';
?>

timer.java:

public class timer {
  public static void main(String args[])
    throws InterruptedException {
     for (int i = 0; i <= 100; i++) {
      //Print a message
      System.out.println("Progress = "+i+"% complete");
      //Pause
      Thread.sleep(150);            
    }
  }
}
Community
  • 1
  • 1
user3349492
  • 23
  • 1
  • 4
  • How can you use java with php? – Lal krishnan S L Feb 25 '14 at 04:43
  • Calling server-side Java from PHP is straightforward using the exec(), passthru(), system() or proc_open() functions. I do this to take advantage of Java's great computational capabilities (matrix inversion etc). But the question is about displaying the Java output stream in the browser in real time, not how to call it in the first place. – user3349492 Feb 25 '14 at 04:49

0 Answers0