简体   繁体   English

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

[英]How to execute java command from php page with shell_exec() function?

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.我正在尝试使用函数 shell_exec() 从 PHP 页面运行 Java 程序,问题是总是返回空响应。 When run a command like: ls, whoami, which, it works.当运行如下命令时:ls、whoami、which,它起作用了。 I think that is related to grant access for the www-data user, but I still can not find a way to do it.我认为这与授予 www-data 用户访问权限有关,但我仍然找不到方法。

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:我尝试使用命令sudo visudo修改文件/etc/sudoers ,以便在文件末尾添加下一行,假设授予执行 java 命令的访问权限:

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

and tried with this too:并尝试过这个:

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

I'm using next code to invoke the java command from the php page:我正在使用下一个代码从 php 页面调用 java 命令:

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

echo $result;回声 $result;

I expect the output of the invoked command, but the actual output is empty.我期望调用命令的输出,但实际输出为空。

thanks in advance.提前致谢。

I just tested and it's working我刚刚测试过,它正在工作

<?php 
    $out = shell_exec('java 2>&1');
    echo '<pre>'.$out.'</pre>';
?> 

Reference : In the shell, what does " 2>&1 " mean?参考:在shell中,“2>&1”是什么意思?

This might work.这可能会奏效。

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

executes only one line of java code.只执行一行java代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM