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);
}
}
}