PHP/Java桥问题 [英] PHP/Java bridge problem

查看:54
本文介绍了PHP/Java桥问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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("<?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

我想念什么?

推荐答案

只需将其添加到您的命令行中即可:

Just add this to your command line:

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

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

问题解决了!

这篇关于PHP/Java桥问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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