PHP/Java 桥接问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2728632/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:33:17  来源:igfitidea点击:

PHP/Java bridge problem

javaphp

提问by Bruce

I am using tomcat 6 on windows. Here is the code I am testing.

我在 Windows 上使用 tomcat 6。这是我正在测试的代码。

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.StringReader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;


/**
* Create and run THREAD_COUNT PHP threads, concurrently accessing a
* shared resource.
*
* Create 5 script engines, passing each a shared resource allocated
* from Java. Each script engine has to implement Runnable.
*
* Java accesses the Runnable script engine using
* scriptEngine.getInterface() and calls thread.start() to invoke each
* PHP Runnable implementations concurrently.
*/
class PhpThreads {

    public static final String runnable = new String("<?php\n" +
            "function run() {\n" +
            " $out = java_context()->getAttribute('sharedResource', 100);\n" +
            " $nr = (string)java_context()->getAttribute('nr', 100);\n" +
            " echo \"started thread: $nr\n\";\n" +
            " for($i=0; $i<100; $i++) {\n" +
            "  $out->write(ord($nr));\n" +
            "  java('java.lang.Thread')->sleep(1);\n" +
            " }\n" +
            "}\n" +
            "?>\n");


                static final int THREAD_COUNT = 5;
    public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    Thread threads[] = new Thread[THREAD_COUNT];
    ScriptEngine engines[] = new ScriptEngine[THREAD_COUNT];
    ByteArrayOutputStream sharedResource = new ByteArrayOutputStream();
    StringReader runnableReader = new StringReader(runnable);

    // create THREAD_COUNT PHP threads
    for (int i=0; i<THREAD_COUNT; i++) {
        engines[i] = manager.getEngineByName("php-invocable");
        if (engines[i] == null)
        throw new NullPointerException ("php script engine not found");

        engines[i].put("nr", new Integer(i+1));
        engines[i].put("sharedResource", sharedResource);

        engines[i].eval(runnableReader);
        runnableReader.reset();

        // cast the whole script to Runnable; note also getInterface(specificClosure, type)
        Runnable r = (Runnable) ((Invocable)engines[i]).getInterface(Runnable.class);
        threads[i] = new Thread(r);
    }

    // run the THREAD_COUNT PHP threads
    for (int i=0; i<THREAD_COUNT; i++) {
        threads[i].start();
    }

    // wait for the THREAD_COUNT PHP threads to finish
    for (int i=0; i<THREAD_COUNT; i++) {
        threads[i].join();
        ((Closeable)engines[i]).close();
    }

    // print the output generated by the THREAD_COUNT concurrent threads
    String result = sharedResource.toString();
    System.out.println(result);

    // Check result
    Object res=manager.getEngineByName("php").eval(
        "<?php " +
        "exit((int)('10011002100310041005'!=" +
        "@system(\"echo -n "+result+"|sed 's/./&\\n/g'|sort|uniq -c|tr -d ' \\n'\")));" +
        "?>");

    System.exit(((Number)res).intValue());
    }
}

I have added all the libraries. When I run the file I get the following error -

我已经添加了所有的库。当我运行该文件时,出现以下错误 -

run:
Exception in thread "main" javax.script.ScriptException: java.io.IOException: Cannot run program "php-cgi": CreateProcess error=2, The system cannot find the file specified
        at php.java.script.InvocablePhpScriptEngine.eval(InvocablePhpScriptEngine.java:209)
        at php.java.script.SimplePhpScriptEngine.eval(SimplePhpScriptEngine.java:178)
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
        at PhpThreads.main(NewClass.java:53)
Caused by: java.io.IOException: Cannot run program "php-cgi": CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
        at java.lang.Runtime.exec(Runtime.java:593)
        at php.java.bridge.Util$Process.start(Util.java:1064)
        at php.java.bridge.Util$ProcessWithErrorHandler.start(Util.java:1166)
        at php.java.bridge.Util$ProcessWithErrorHandler.start(Util.java:1217)
        at php.java.script.CGIRunner.doRun(CGIRunner.java:126)
        at php.java.script.HttpProxy.doRun(HttpProxy.java:63)
        at php.java.script.CGIRunner.run(CGIRunner.java:111)
        at php.java.bridge.ThreadPool$Delegate.run(ThreadPool.java:60)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
        at java.lang.ProcessImpl.start(ProcessImpl.java:30)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
        ... 8 more

What am I missing?

我错过了什么?

回答by Brent Ryan

Just add this to your command line:

只需将此添加到您的命令行:

-Dphp.java.bridge.php_exec=/usr/bin/php

-Dphp.java.bridge.php_exec=/usr/bin/php

Problem solved!

问题解决了!

回答by sulaiman sudirman

copy the correct VERSION of the PHP of php5ts.dll and php-cgi.exe to "WEB-INF\cgi\amd64-windows" directory. then restart Tomcat. good luck.

将php5ts.dll和php-cgi.exe的正确版本的PHP复制到“WEB-INF\cgi\amd64-windows”目录下。然后重启Tomcat。祝你好运。

回答by symcbean

...php-cgi...The system cannot find the file specified

...php-cgi...系统找不到指定的文件

I'm guessing that manager.getEngineByName("php-invocable") should return a wrapper around a system call to run PHP - but that wrapper doesn't know where to find the PHP executable.

我猜 manager.getEngineByName("php-invocable") 应该返回一个围绕系统调用运行 PHP 的包装器 - 但该包装器不知道在哪里可以找到 PHP 可执行文件。

A quick glance at the website for the PHP/Java bridge, and I infer that the path is hard coded in the Java - "For further information please see the INSTALL.J2EE file from the documentation download"

快速浏览一下 PHP/Java 桥的网站,我推断该路径是用 Java 硬编码的 - “有关更多信息,请参阅文档下载中的 INSTALL.J2EE 文件”

The Javadoc is decidedly vague on the topic.

Javadoc 在这个主题上非常含糊。

You need to specifically make the -cgi version of PHP at compile time, assuming you've done that, and it is called php-cgi, then as a quick hack you could perpper your filesystem with links named "php-cgi" (its probably expected to be in /bin, /usr/bin/, /usr/local/bin, or the Java may be smart enough to check $PATH)

您需要在编译时专门制作 PHP 的 -cgi 版本,假设您已经这样做了,并且它被称为 php-cgi,然后作为一个快速的 hack,您可以使用名为“php-cgi”(它的可能应该在 /bin、/usr/bin/、/usr/local/bin 中,或者 Java 可能足够聪明以检查 $PATH)

C.

C。

回答by Dharmesh Patel

when you get error like

当你得到这样的错误时

Fatal Error: Failed to start PHP ["php-cgi", "-v"], reason: java.io.IOException:
 Cannot run program ""php-cgi"" (in directory "C:\Documents and Settings\Adminis
trator"): CreateProcess error=2, The system cannot find the file specified
Could not start FCGI server: java.io.IOException: PHP not found. Please install
php-cgi. PHP test command was: [php-cgi, -v]
php.java.bridge.http.FCGIConnectException: Could not connect to server
        at php.java.bridge.http.NPChannelFactory.test(NPChannel`enter code here`Factory.java:64)
        at php.java.bridge.http.FCGIConnectionPool.<init>(FCGIConnectionPool.jav
a:175)
        at php.java.bridge.http.FCGIConnectionPool.<init>(FCGIConnectionPool.jav
a:189)
        at php.java.servlet.ContextLoaderListener.createConnectionPool(ContextLo
aderListener.java:541)
        at php.java.servlet.ContextLoaderListener.contextInitialized(ContextLoad
erListener.java:185)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContex
t.java:4135)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4
630)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
.java:791)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:77
1)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)

        at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.jav
a:1041)
        at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.j
ava:964)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502
)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
:321)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
eSupport.java:119)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)

        at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)

        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445
)
        at org.apache.catalina.core.StandardService.start(StandardService.java:5
19)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:710
)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.io.IOException: File \.\pipe\C:\Documents and Settings\Administ
rator\Desktop\softwares\apache-tomcat-6.0.29\temp\JavaBridge3144995283109409611.
socket not writable
        at php.java.bridge.http.FCGIConnectException.<init>(FCGIConnectException
.java:37)
        ... 29 more
Caused by: java.io.IOException: PHP not found. Please install php-cgi. PHP test
command was: [php-cgi, -v]
        at php.java.bridge.Util$Process.start(Util.java:1145)
        at php.java.servlet.fastcgi.FCGIProcess.start(FCGIProcess.java:68)
        at php.java.bridge.http.NPChannelFactory.doBind(NPChannelFactory.java:94
)
        at php.java.bridge.http.FCGIConnectionFactory.runFcgi(FCGIConnectionFact
ory.java:88)
        at php.java.bridge.http.FCGIConnectionFactory.run(FCGIConnectionFactor
y.java:109)

with the JavaBridge.war deployment in (windows, tomcat)

使用 JavaBridge.war 部署(windows、tomcat)

please specify the pathfor the php installation in your environment variable.

请在您的环境变量中指定php 安装的路径