我想用PHP代码执行java程序。当我编写下面的代码时,编辑器中编写的程序将被编译并成功执行。
putenv('path=C:\Program Files\Java\jdk1.8.0_73\bin');
echo shell_exec("javac $output 2>&1"); // for compiling and creating class file
echo exec("java $check 2>&1"); // for executing generated class file and prints output
注意:$output
和$check
变量包含编译所需的输入和运行
但是,当我尝试执行java程序时,我们需要输入一些数据来执行,这些代码不起作用。例如,请考虑链接中的JavaScannerExample
:http://alvinalexander.com/java/edu/pj/pj010005
要在链接中执行程序,我想我需要一些交互式控制台来接受输入并继续执行程序。请建议我如何使用PHP实现这一目标?
更新
@Chris ..按照建议尝试proc_open
命令。请参阅下面的代码:
PHP代码
<?php
$JAVA_HOME = "C:\Java\jdk1.7.0_79";
$PATH = "$JAVA_HOME\bin";
putenv("JAVA_HOME=$JAVA_HOME");
putenv("PATH=$PATH");
$result = shell_exec("javac JavaScannerExample.java 2>&1");
if ($result == ""){
$options = ["bypass_shell" => true];
$proc=proc_open("java JavaScannerExample",
array(
array("pipe","r"),
array("pipe","w"),
array("pipe","w")
),
$pipes,NULL,NULL,$options);
if (is_resource($proc)) {
fwrite($pipes[0], 'Mark');
fwrite($pipes[0], 32);
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
}
}
else{
echo $result;
}
?>
Java代码
import java.util.Scanner;
public class JavaScannerExample
{
public static void main (String[] args)
{
// create a scanner so we can read the command-line input
Scanner scanner = new Scanner(System.in);
// prompt for the user's name
System.out.print("Enter your name: ");
// get their input as a String
String username = scanner.next();
// prompt for their age
System.out.print("Enter your age: ");
// get the age as an int
int age = scanner.nextInt();
System.out.println(String.format("%s, your age is %d", username, age));
}
}
PHP输出
Enter your name: Enter your age:
请你检查并告诉我哪里出错了?
UPDATE2:
@Chris ..感谢您的建议。它帮助我获得了输出。
这是修改过的php代码:
putenv('path=C:\Program Files\Java\jdk1.8.0_73\bin');
$result = shell_exec("javac JavaScannerExample.java 2>&1");
if ($result == ""){
$options = ["bypass_shell" => true];
$proc=proc_open("java JavaScannerExample",
array(
array("pipe","r"),
array("pipe","w"),
array("pipe","w")
),
$pipes,NULL,NULL,$options);
if (is_resource($proc)) {
fwrite($pipes[0], "Mark\n");
fwrite($pipes[0], "32\n");
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
}
}
else{
echo $result;
}
这是我得到的结果。
Enter your name: Enter your age: Mark, your age is 32
此类结果存在多个问题。首先,我正在尝试构建一个交互式控制台。因此,对于上面的例子,管道需要并行工作。但是,在关闭管道[0]之前,我无法检索管道[1]。因为如果我在代码中获得了管道[1]的值,程序就会挂起。在这种情况下。首先,我需要能够读取输入“输入您的姓名:”然后输入“Mark”输入。接下来的步骤类似我需要获取文本“输入年龄:”并输入输入“32”。最后我需要获得最终输出“Mark,你的年龄是32”并检测程序是否完成然后关闭两个管道。这可能吗?
答案 0 :(得分:0)
使用proc_open代替exec。它可用于检索stdin
的管道,该管道可用于为Java应用程序提供输入。 proc_open
手册页上的第一个示例演示了如何执行此操作。
答案 1 :(得分:0)
关于您的更新,可以使用fread
代替stream_get_contents
。 fread
应仅读取可用内容,而stream_get_contents
将阻止,直到确保流中不再有可读数据为止。如果你想在程序终止之前获得stdout
,请尝试这样的事情。
$line_limit = 1024;
echo fread($pipes[1], $line_limit);
fwrite($pipes[0], "Mark\n");
echo fread($pipes[1], $line_limit);
fwrite($pipes[0], "32\n");
echo fread($pipes[1], $line_limit);
fclose($pipes[0]);
fclose($pipes[1]);
唯一需要注意的是,如果您在Java程序完成输出之前尝试读取数据,那么您可能无法获得所需的所有输出(可能根本没有)。如果您遇到这种情况,我会在写入和读取之间添加延迟(sleep
)。
如果你想要一个真正的交互式终端,那么你可能不得不产生一个后台线程,不断轮询stdout
以获取更多数据,但在另一个问题中可能是最好的问题。