简体   繁体   English

在localhost上从Java调用时,Php exec无法正常运行

[英]Php exec is not running properly when called from Java on localhost

I have a problem running the exec command from within a php script. 我在php脚本中运行exec命令时遇到问题。
here's the detailed scenario: 这是详细的场景:

  • I have an executable my_exe which runs from command line. 我有一个可执行的my_exe ,它从命令行运行。 This executable uses an environment variable, so I prepend the command with MY_VARIABLE='value' (works fine). 此可执行文件使用环境变量,因此我在命令前加上MY_VARIABLE='value' (工作正常)。

  • I use the exec command to run this executable from a Php script. 我使用exec命令从Php脚本运行此可执行文件。 ex: exec("MY_VARIABLE='value' my_exe"); 例如: exec("MY_VARIABLE='value' my_exe"); (works fine too, tested in a browser) (工作正常,在浏览器中测试)

  • The problem is in this part. 问题出在这一部分。 When I call the php script from a Java program using HttpUrlConnection , I have two cases: if I run the Java program from outside the server (using public IP address) I get the good results, but when I run it from the server (using local IP, form command line ex. " java -jar /path/to/my/jar "), the Php works but the exec doesn't seem to be working properly. 当我使用HttpUrlConnection从Java程序调用php脚本时,我有两种情况:如果我从服务器外部运行Java程序(使用公共IP地址),我会得到很好的结果,但是当我从服务器运行它时(使用本地IP,表单命令行ex。“ java -jar /path/to/my/jar ”),Php工作,但exec似乎没有正常工作。 The environment variable is not being set in this case, so the executable runs but without the right values, so I get no results. 在这种情况下没有设置环境变量,因此可执行文件运行但没有正确的值,因此我没有得到任何结果。

Any help is appreciated. 任何帮助表示赞赏。 I'm really stuck here. 我真的被困在这里了。 My guess is that there is a problem with Php or Java permissions, but I can't figure it out. 我的猜测是Php或Java权限存在问题,但我无法弄清楚。

Strange command exec("MY_VARIABLE='value' my_exe"); 奇怪的命令exec("MY_VARIABLE='value' my_exe"); ... it will work only on linux machine and will not on windows. ...它只能在linux机器上运行,不会在Windows上运行。 As a first step, try to rewrite you php code like this: 作为第一步,尝试重写你的php代码,如下所示:

putenv("MY_VARIABLE=value");
exec("my_exe");

and see what happen. 看看会发生什么。

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

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