如何在 php 脚本中执行 Java 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3737031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I execute a Java program within a php script?
提问by user450775
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:
我正在编写一个简单的网络上传脚本。目标是使用php上传一个文件,然后调用一个java程序来处理这个文件。
我已经完成了上传文件的工作,但我无法从 php 脚本中成功运行 java 程序。
我曾尝试exec()
,shell_exec()
和system()
没有结果。
对于命令,我使用了“ java Test
”、“ java < directory >/Test
”、“ /usr/bin/java < directory >/Test
”,我什至将应用程序设置为没有结果的 jar 文件。我使用的实际代码行是:
echo shell_exec("java Test");
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?
通常没有输出。但是,如果我刚刚shell_exec("java")
,则show splash screen with specified image
显示来自 java (" ")的帮助的最后一行,这表明该命令已被执行。例如,如果我使用 shell_exec("whoami")
我得到“没人”返回,这是正确的。java文件所做的唯一一件事就是创建一个文件,以便我可以看到应用程序已成功运行(如果我在命令行上运行它,则应用程序运行成功)。我已将 java 文件的权限设置为 777 以排除任何权限错误的可能性。我一直在努力解决这个问题,尝试了各种没有结果的选项 - 从未创建过该文件(该文件是使用绝对路径创建的,因此它没有被创建,我只是找不到该文件)。有没有人有任何想法?
Thanks.
谢谢。
回答by Dr Casper Black
I dont realy know, but i came a cross PHP-JAVA bridge maybe it can help
我真的不知道,但我来到了一个跨 PHP-JAVA 桥,也许它可以帮助
http://php-java-bridge.sourceforge.net/pjb/
http://php-java-bridge.sourceforge.net/pjb/
Update:
更新:
I tested this with Jasper Reports, and it is working really nice. It will allow you to Extend Java classes with PHP or just use Java class lik it was PHP.
我用 Jasper Reports 对此进行了测试,效果非常好。它将允许您使用 PHP 扩展 Java 类,或者像 PHP 一样使用 Java 类。
use java\lang\String as JString;
require_once("javabridge/java/Java.inc");
class String extends JString {
function toString () {
return "hello " . parent::toString();
}
}
$str = new String("Java");
echo $str->toString();
or
或者
$temp = new Java('java.sql.Timestamp');
$javaObject = $temp->valueOf('2007-12-31 0:0:0');
$params = new Java("java.util.HashMap");
$params->put("text", "This is a test string");
$params->put("date",$javaObject);
More examples: http://php-java-bridge.sourceforge.net/pjb/FAQ.html
回答by AnArcticPuffin
It's possible it has to do with the path that the exec is defaulting to. You may need to explicitly define your classpath with an absolute path to your .class or jar files when calling java.
这可能与 exec 默认的路径有关。调用 java 时,您可能需要使用 .class 或 jar 文件的绝对路径显式定义类路径。
回答by Alfred
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?
我一直在努力解决这个问题,尝试了各种没有结果的选项 - 从未创建过该文件(该文件是使用绝对路径创建的,因此它没有被创建,我只是找不到该文件)。有没有人有任何想法?
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.
我认为问题是什么。Apache 作为“nobody”组运行??(apache 用户??) 它将执行java 脚本,该脚本将尝试在磁盘上的某处创建文件。我认为它无权写入该位置。您应该 chown 该文件夹,以便 apache 用户可以写入该文件夹。
==
==
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.
首先,我想向您指出,从脚本中调用 exec() 真的会炸毁您的服务器。我建议您改用 redis(见下文)之类的东西。
==
==
Second I think I know what the problem is. You should first try to run the simple example below which worked fine for me.
其次,我想我知道问题是什么。您应该首先尝试运行下面的简单示例,它对我来说效果很好。
==
==
First be sure permission are set right. Because apache runs as nobody(most of the times).
首先确保权限设置正确。因为 apache 以无人身份运行(大多数情况下)。
I tried this simple test myself on ubuntu with php installed from repo.
我自己在 ubuntu 上尝试了这个简单的测试,并从 repo 安装了 php。
test.java
测试.java
class test {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
test.php
测试文件
echo exec('java test');
Ran test.php
跑 test.php
$ php test.php
Hello World!
==
==
Or you could try 1 of the following solutions(which would even be a better solution):
或者您可以尝试以下解决方案之一(这甚至是更好的解决方案):
- 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()
- Do a blocking pop/pushfrom 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.
- Use a JMSlike 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.
- 将您的 Java 程序编写为网络服务,例如在大气-铲-服务器(简单/嵌入式 jar)之上。这可以写得非常快。但是在高负载下,我猜这不是最佳选择。我仍然认为这对你来说可能已经足够快了。即使这样,它的执行速度也会快得多,因为您不会有运行 JVM 的开销。可能炸毁你的服务器,不如 exec() 快
- 从redis(*nix) 列表结构执行阻塞弹出/推送。这将很容易在 *nux 上编写,因为 java/php 都有客户端库。我猜速度会最好,因为 redis 是用 C 编写的。我自己使用 redis。
- 使用JMS,例如activemq。也很容易编写,因为良好的库支持。我自己没有使用过 JMS。我使用redis解决方案。我想速度会比使用 redis 解决方案低一点。
回答by Juliette
<?php
$PATH="C:\Program Files\Java\jdk1.7.0_09\bin";
echo exec("javac theNameOfYourJavaProgram.java 2>&1");//shows # of errors
echo "<br />";
echo exec("java theNameOfYourJavaProgram 2>&1");//this line executes it
echo "<br />";
echo shell_exec("javac theNameOfYourJavaProgram.java 2>&1 ");//compiles it
?>