如何使用shell_exec()函数从PHP页面执行Java命令? [英] How to execute java command from php page with shell_exec() function?

查看:56
本文介绍了如何使用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.

这篇关于如何使用shell_exec()函数从PHP页面执行Java命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆