PHP / Java köprü sorunu

4 Cevap java

Ben windows tomcat 6 kullanıyorum. İşte test ediyorum kodudur.

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());
    }
}

Ben bütün kütüphaneleri ekledik. Ben dosyayı çalıştırdığınızda aşağıdaki hatayı alıyorum -

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

Ne eksik?

4 Cevap

Sadece komut satırına bunu ekleyin:

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

Sorun çözüldü!

"WEB-INF \ cgi \ amd64-pencere" dizinine php5ts.dll ve php-cgi.exe ve PHP doğru VERSION kopyalayın. sonra Tomcat yeniden başlatın. iyi şanslar.

... Php-cgi ... Sistem belirtilen dosyayı bulamıyor

Ben manager.getEngineByName ("php-invocable") PHP çalıştırmak için bir sistem çağrısı etrafında sarıcı dönmesi gerektiğini tahmin ediyorum - ama bu sarıcı nereye PHP yürütülebilir bulmak bilmiyor.

PHP / Java köprü için web sitesi, ve ben yolu sert Java kodlu olduğu sonucuna hızlı bir bakışta - "Daha fazla bilgi için belgelere indirmeden INSTALL.J2EE dosyasına bakın lütfen"

Javadoc konu üzerinde kesinlikle belirsiz.

Eğer o hızlı kesmek gibi ("php-cgi" adlı bağlantıları ile dosya sistemi perpper olabilir, yaptık, ve php-cgi denir varsayarak, özellikle derleme zamanında PHP-cgi sürüm yapmak gerekir, onun muhtemelen / bin olması bekleniyor, / usr / bin /, / usr / local / bin, veya Java) $ PATH kontrol etmek akıllı olabilir

C.

Eğer gibi hata alıyorum ne zaman

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$1.run(FCGIConnectionFactor
y.java:109)

(windows tomcat) olarak JavaBridge.war dağıtım ile

Lütfen environment variable olarak php kurulum için path belirtiniz.