如何在php脚本中执行Java程序? [英] How can I execute a Java program within a php script?

查看:131
本文介绍了如何在php脚本中执行Java程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在撰写一个简单的网络上传脚本。
目标是使用php上传文件,然后调用java程序来处理这个文件。

我已经完成了上传文件的工作,但我无法获得java程序从PHP脚本成功运行。

我试过 exec() shell_exec() system()没有结果。

对于命令,我使用了 java Test java< directory> / Test / usr / bin / java< directory> / Test ,我甚至将应用程序设置为一个没有结果的jar文件。我使用的实际代码行是:

I am writing a simple web upload script. The goal is to upload a file using php, and then calling a java program to process this file.
I have done the work for uploading the file, but I cannot get a java program to be successfully run from within the php script.
I have tried exec(), shell_exec(), and system() with no results.
For the command, I have used "java Test", "java < directory >/Test", "/usr/bin/java < directory >/Test", I have even set up the application as a jar file with no results. The actual line of code I have used is:

echo shell_exec("java Test");

通常没有输出。但是,如果我只有 shell_exec(java),那么java的帮助的最后一行(显示带有指定图像的启动画面<显示/ code>),表示该命令已执行。如果我使用,例如,s hell_exec(whoami)我返回nobody,这是正确的。 java文件唯一要做的就是创建一个文件,以便我可以看到应用程序已成功运行(如果我在命令行上运行它,应用程序就会成功运行)。我已将java文件的权限设置为777,以排除任何权限错误的可能性。我一直在努力尝试各种各样的选项而没有结果 - 文件永远不会被创建(文件是用绝对路径创建的,所以它没有被创建,我只是找不到文件)。有没有人有任何想法?

Usually there is no output. However, if I have just shell_exec("java"), then the last line of the help from java ("show splash screen with specified image") is displayed, which shows that the command has been executed. If I use, for example, shell_exec("whoami") I get "nobody" returned, which is correct. The only thing the java file does is create a file so that I can see that the application has been successfully run (the application runs successfully if I run it on the command line). I have set the permissions for the java file to 777 to rule out any possibility of permission errors. I have been struggling with this for a while trying all sorts of options with no results - the file is never created (the file is created with an absolute path so it's not being created and I just can't find the file). Does anyone have any ideas?

谢谢。

推荐答案


我一直在为这个
苦苦挣扎,同时用
尝试各种选项没有结果 - 文件永远不会被创建
(文件是用绝对$ b创建的$ b路径所以它没有被创建,而我
只是找不到文件)。有没有人
有什么想法?

I have been struggling with this for a while trying all sorts of options with no results - the file is never created (the file is created with an absolute path so it's not being created and I just can't find the file). Does anyone have any ideas?

我认为问题是什么。 Apache运行为nobody组(apache user ??),它将执行java脚本,该脚本将尝试在某处创建光盘上的文件。我认为它没有写入该位置的权限。你应该知道该文件夹,以便apache用户可以写入该文件夹。

What I think the problem is. Apache runs as "nobody" group??(apache user??) which will execute the java script which will try to create a file on disc somewhere. I assume it does not have permission to write to that location. you should chown that folder so that apache user can write to that folder.

==

首先关闭我我想向你指出,从脚本中调用exec()可能真的会炸毁你的服务器。我会建议你使用像redis这样的东西(见下文)。

First off I would like to point out to you that calling exec() from a script could really blow up your server. I would advice you to use something like redis(see below) instead.

==

其次我认为我知道问题是什么。你应该首先尝试运行下面对我来说很好的简单示例。

Second I think I know what the problem is. You should first try to run the simple example below which worked fine for me.

==

首先是确定权限设置正确。因为apache作为无人运行(大多数时候都是这样)。

First be sure permission are set right. Because apache runs as nobody(most of the times).

我在ubuntu上尝试了这个简单的测试,从repo安装了php。

I tried this simple test myself on ubuntu with php installed from repo.

test.java

test.java

class test {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

test.php


test.php

echo exec('java test');

Ran test.php

Ran test.php

$ php test.php
Hello World!

==

或者你可以试试以下解决方案中的一个(这甚至是更好的解决方案):

Or you could try 1 of the following solutions(which would even be a better solution):


  1. 将您的java程序编写为web服务,例如在大气层之上-spade-server(简单/嵌入式jar)。这可能是疯狂写的。但在高负荷下,这不是我猜的最佳选择。我认为这对你来说还不够快。即使这样,执行它也会快得多,因为你不会有运行JVM的开销。 可能会炸毁你的服务器,而不是像exec那样快()

  2. 做一个 redis的7 / blocking /rel =noreferrer>阻止弹出/推送(* nix)列表结构。这将非常容易在* nux上编写,因为有两个java / php的客户端库。速度最好我猜,因为redis是用C语言编写的。我自己使用redis。

  3. 使用 JMS ,例如 activemq 。也很容易写,因为良好的库支持。我自己没有使用过JMS。我使用redis解决方案。我猜的速度会比redis解决方案少一点。

  1. Write your java program as a webservice for example on top of atmosphere-spade-server(simple/embedded jar). This could be written insanely fast. But on high load this will not be best option I guess. Still I think this will be more than fast enough for you probably. Even this way it will be much faster as executing it, because you won't have the overhead running JVM. Could blow up your server, not as fast as exec()
  2. Do a blocking pop/push from a redis(*nix) list structure. This will be pretty easy to write on *nux because there are client libraries for both java/php. The speed will best I guess because redis is written in C. I use redis myself.
  3. Use a JMS like for example activemq. Also pretty easy to write because good library support. I have not used a JMS myself. I use redis solution. The speed I guess would be a little less then with redis solution.

这篇关于如何在php脚本中执行Java程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆