PHP/Java bridge problem

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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

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("getAttribute('sharedResource', 100);\n" +             " $nr = (string)java_context()->getAttribute('nr', 100);\n" +             " echo \"started thread: $nr\n\";\n" +             " for($i=0; $iwrite(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");      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.(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?

回答1:

Just add this to your command line:

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

Problem solved!



回答2:

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.



回答3:

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

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.

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"

The Javadoc is decidedly vague on the topic.

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)

C.



回答4:

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.(FCGIConnectionPool.jav a:175)         at php.java.bridge.http.FCGIConnectionPool.(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.(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) 

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

please specify the path for the php installation in your environment variable.



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