简体   繁体   English

在 PHP 中运行 shell_exec('which java') 不返回任何内容

[英]Running shell_exec('which java') in PHP return nothing

If I run如果我跑

which java

from the command line I get the proper input (/usr/java/.../bin/java).从命令行我得到正确的输入(/usr/java/.../bin/java)。 However if I run it in a php script:但是,如果我在 php 脚本中运行它:

<?
  echo 'java. ' . shell_exec('which java');
  echo 'ls. ' . shell_exec('which ls');
?>

nothing gets printed out for which java but I get the proper results for which ls...没有打印出哪个 java 但我得到了哪个 ls 的正确结果...

Two things were needed:需要做两件事:

  • the full path to the JVM (it wasn't set in the environment) JVM 的完整路径(未在环境中设置)
  • " 2>&1" at the end of the command line.命令行末尾的“2>&1”。

So for example:例如:

echo shell_exec('/usr/java/jdk6/bin/java -cp myJars.jar MyMainClass arg1 2>&1");

I just ran into this problem as well.我也刚遇到这个问题。 I was trying to determine if the qrencode utility is installed on the (any) server, and if not then log/warn/exit gracefully.我试图确定qrencode实用程序是否安装在(任何)服务器上,如果没有,则正常登录/警告/退出。

Considering the program should always be in a standard path, I prefixed the which command with the likely locations of the binary, while still respecting that $PATH might be somehow defined on the system.考虑到程序应该始终在标准路径中,我在which命令前面加上了二进制文件的可能位置,同时仍然考虑到$PATH可能以某种方式在系统上定义。 I think you could use this approach with common Java paths, too:我认为您也可以将这种方法用于常见的 Java 路径:

shell_exec('PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin" '.
           'which qrencode');

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

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