简体   繁体   English

无法在Ubuntu上通过PHP执行Java jar文件

[英]Can't execute a Java jar file via PHP on Ubuntu

Here is my PHP code: 这是我的PHP代码:

<?php
exec('java -jar ~/src/epubcheck-*/epubcheck-*.jar -out /var/www/epubcheck-outputs/output.xml /var/www/AChristmasCarol.epub');

When I try the command in the terminal it works... But in PHP it does not. 当我在终端中尝试命令时它可以正常工作......但是在PHP中却没有。

Also I don't even get the java version. 我甚至没有得到java版本。 But I do get to see the "hey..." so some commands do work, others don't. 但我确实看到了“嘿......”所以有些命令可以正常工作,有些则无法运行。

I am using NGiNX has my server. 我正在使用NGiNX有我的服务器。

The documentation for exec explains that only the last line of the output is returned by exec, so you need to use a parameter to capture the full output. exec的文档解释了exec只返回输出的最后一行,因此您需要使用参数来捕获完整输出。

java -version sends its output to STDERR , not STDOUT , so you need to redirect STDERR to STDOUT if you want to capture the output of that command with PHP. java -version将其输出发送到STDERR ,而不是STDOUT ,因此如果要使用PHP捕获该命令的输出,则需要将STDERR重定向到STDOUT However, this shouldn't be necessary when you're running an ordinary java program. 但是,当您运行普通的Java程序时,这不是必需的。

$output = array();

exec('java -version 2>&1', $output);
print_r($output); // contains the correct output

exec('java -jar myfile.jar', $output);
print_r($output); // should also contain the correct output

If this still doesn't work, see my comment on Ibu's answer 如果这仍然不起作用,请参阅我对Ibu答案的评论

Edit: Actual answer, from question comments: 编辑:实际答案,来自问题评论:

~/src/epubcheck-*/epubcheck-*.jar could be a problem here - ~ is a shortcut for the home directory of the current user - so when you run the command yourself, it means /home/username/src/... , but when you run it as your webserver's user, it means an entirely different path. ~/src/epubcheck-*/epubcheck-*.jar可能是一个问题 - ~当前用户主目录的快捷方式 - 所以当你自己运行命令时,它意味着/home/username/src/... ,但是当你作为网络服务器的用户运行它时,它意味着完全不同的路径。 Try changing that to the full path of the jar files you want to execute. 尝试将其更改为要执行的jar文件的完整路径。

On the php manual it says that passthru returns void: 在PHP手册上,它说passthru返回void:

void passthru ( string $command [, int &$return_var ] ) void passthru(string $ command [,int&$ return_var])

So nothing will be echoed. 所以什么都不会回应。

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

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