Execute java class in PHP

后端 未结 4 1928
眼角桃花
眼角桃花 2021-01-16 01:50

I want to call a java program and fetch it\'s output in stdout. I followed the suggestions in stackoverflow. But it doesn\'t work.

I have add the class file to my C

相关标签:
4条回答
  • 2021-01-16 02:24

    Try this:

    exec('java -cp .:/path/to/folder/of/your/file Hello 2>&1', $output);
    print_r($output);
    

    the 2>&1 need to display errors.

    0 讨论(0)
  • 2021-01-16 02:28

    I would recommend using Java/PHP Bridge found here: http://php-java-bridge.sourceforge.net/pjb/ It's quite easy to install and works very well.

    Also, I recommend using the following link to download it. (it's the same one as the link in downloads->documentation)

    http://sourceforge.net/projects/php-java-bridge/files/Binary%20package/php-java-bridge_6.2.1/php-java-bridge_6.2.1_documentation.zip/download

    The file is JavaBridge.war. You'll probably want to use Tomcat for the Java EE container. Once Tomcat is set up, you just put this file in the webapps folder and it's installed.

    If you want to regularly use java classes in PHP this is the best method I know of and I have tried a lot of them. Resin also worked, but it didn't play nice with my mail server.

    0 讨论(0)
  • 2021-01-16 02:32

    Well, it yields array right? so instead print_r($output) try print($output[0]), that outputting 'Hello World' on my console :D

    0 讨论(0)
  • 2021-01-16 02:32

    try pipe

    $command = 'java Hello';
    $descriptorspec = array(
        1 => array(
            'pipe', 'w'
        )
    );
    $process = proc_open($command, $descriptorspec, $pipes);
    if (!is_resource($process)) {
        exit("failed to create process");
    }
    $content = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    if (proc_close($process) === 0) {
        print_r($content);
    }else{
        exit("failed to execute Hello");
    }
    
    0 讨论(0)
提交回复
热议问题