简体   繁体   English

exec()php总是返回数组(size = 0)为空吗?

[英]exec() php always returning array (size=0) empty?

I'm trying to execute jar file in my php program. 我正在尝试在我的php程序中执行jar文件。 I'm testing with this simple example : 我正在用这个简单的例子进行测试:

exec('java -jar "C:\wamp\www\Hello.jar" myName', $output);
var_dump($output);

but it always printing 但它总是打印

array (size=0)
  empty

BTW the jar file does System.out.println("hello "+args[0]); 顺便说一句,jar文件执行System.out.println("hello "+args[0]); and I'm testing it in cmd and it is working fine. 我正在cmd中对其进行测试,并且工作正常。

Please help I'm struggling with this problem since two days. 自两天以来,请帮助我解决这个问题。

try this: 尝试这个:

exec('java -jar \"C:\wamp\www\Hello.jar" myName', $output);

instead of 代替

exec('java -jar "C:\wamp\www\Hello.jar" myName', $output);

尝试

exec("/path_to_java -jar C:\wamp\www\Hello.jar myName", $output); 
  1. You have to escape backslashes: replace \\ with \\\\ 您必须转义反斜杠:用\\\\替换\\
  2. You should build CMD and JAR strings separately. 您应该分别构建CMD和JAR字符串。 (Good programming) (好的编程)
  3. Redirect errors to output with 2>&1 to display errors in output. 使用2>&1将错误重定向到输出以显示输出中的错误。

Simple: 简单:

$JAR = 'C:\\wamp\\www\\Hello.jar';
$CMD = sprintf(
    'java -Xmx32m -jar %s myName 2>&1'
    ,escapeshellarg($JAR)
);
exec($CMD, $Output, $Status);
var_dump($CMD, $Output, $Status);

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

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