why exec('java -jar file.jar') dont work on browser but works on command line?

时光毁灭记忆、已成空白 提交于 2019-12-08 09:35:16

问题


I try to run an jar from an php file like this:

exec("java -jar /home/florinbuda/NetBeansProjects/monkey1/dist/monkey1.jar", $result);          
print_r($result);

and IT WORKS if I run it from command line like:

$ php runner.php

but it doesn't work if I try to load it via http-localhost-server/runner.php the page just keeps loading forever without giving any error..

In the .jar file I added a simple test to now if the jar is even started to work

public static void main(String[] args) {
    new File("/home/florinbuda/Desktop/xxx").mkdir();

and as you can guess - when I call http-localhost-server/runner.php the jar is not even started to be executed...

It's a problem of rights? What suggestions do you have?


回答1:


Yes, it's the problem of permission.

You should chmod the parent folder of .jar file.

chmod 755 -R /home/florinbuda/NetBeansProjects/monkey1/dist/

EDIT: And then your script should look like this:

exec("PATH_TO/java -jar /home/florinbuda/NetBeansProjects/monkey1/dist/monkey1.jar");



回答2:


The environment variables in bash aren't the same in exec(). Use the full path for java bin at least. You could also try just to run /path/to/java to see the normal output is working.



来源:https://stackoverflow.com/questions/13368155/why-execjava-jar-file-jar-dont-work-on-browser-but-works-on-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!