如何使用shell_exec()函数从PHP页面执行Java命令?

如何使用shell_exec()函数从PHP页面执行Java命令?

问题描述:

我正在尝试使用功能shell_exec()从PHP页面运行Java程序,问题是总是返回空响应.运行以下命令时:ls,whoami,该命令有效.我认为这与为www数据用户授予访问权限有关,但我仍然找不到解决方法.

I'm trying to run a Java program from a PHP page, using the function shell_exec(), the problem is that always return an empty response. When run a command like: ls, whoami, which, it works. I think that is related to grant access for the www-data user, but I still can not find a way to do it.

我尝试使用命令 sudo visudo 修改文件/etc/sudoers ,以便在文件末尾添加应该授予访问权限的下一行执行Java命令:

I tried to modify the file /etc/sudoers with the command sudo visudo, for add to the end of the file the next line that suppose to grant access for execute the java command:

www-data ALL = NOPASSWD:/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

www-data ALL=NOPASSWD: /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

也尝试过:

www-data ALL = NOPASSWD:/usr/bin/java

www-data ALL=NOPASSWD: /usr/bin/java

我正在使用下一个代码从php页面调用Java命令:

I'm using next code to invoke the java command from the php page:

$ result = shell_exec('java -version');

$result = shell_exec('java -version');

echo $ result;

echo $result;

我希望已调用命令的输出,但是实际输出为空.

I expect the output of the invoked command, but the actual output is empty.

提前谢谢.

这可能有效.

echo shell_exec("echo " . $command_to_exec . " > myfile.java && javac myfile.java && java myfile.java");

仅执行一行Java代码.

executes only one line of java code.