How can I execute a Java program within a php script?

后端 未结 4 389
慢半拍i
慢半拍i 2021-01-02 22:02

I am writing a simple web upload script. The goal is to upload a file using php, and then calling a java program to process this file.
I have done the work for uploading

相关标签:
4条回答
  • 2021-01-02 22:44

    I have been struggling with this for a while trying all sorts of options with no results - the file is never created (the file is created with an absolute path so it's not being created and I just can't find the file). Does anyone have any ideas?

    What I think the problem is. Apache runs as "nobody" group??(apache user??) which will execute the java script which will try to create a file on disc somewhere. I assume it does not have permission to write to that location. you should chown that folder so that apache user can write to that folder.

    ==

    First off I would like to point out to you that calling exec() from a script could really blow up your server. I would advice you to use something like redis(see below) instead.

    ==

    Second I think I know what the problem is. You should first try to run the simple example below which worked fine for me.

    ==

    First be sure permission are set right. Because apache runs as nobody(most of the times).

    I tried this simple test myself on ubuntu with php installed from repo.

    test.java

    class test {
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
    

    test.php

    echo exec('java test');
    

    Ran test.php

    $ php test.php
    Hello World!
    

    ==

    Or you could try 1 of the following solutions(which would even be a better solution):

    1. Write your java program as a webservice for example on top of atmosphere-spade-server(simple/embedded jar). This could be written insanely fast. But on high load this will not be best option I guess. Still I think this will be more than fast enough for you probably. Even this way it will be much faster as executing it, because you won't have the overhead running JVM. Could blow up your server, not as fast as exec()
    2. Do a blocking pop/push from a redis(*nix) list structure. This will be pretty easy to write on *nux because there are client libraries for both java/php. The speed will best I guess because redis is written in C. I use redis myself.
    3. Use a JMS like for example activemq. Also pretty easy to write because good library support. I have not used a JMS myself. I use redis solution. The speed I guess would be a little less then with redis solution.
    0 讨论(0)
  • 2021-01-02 22:55

    It's possible it has to do with the path that the exec is defaulting to. You may need to explicitly define your classpath with an absolute path to your .class or jar files when calling java.

    0 讨论(0)
  • 2021-01-02 23:03
    <?php
    $PATH="C:\Program Files\Java\jdk1.7.0_09\bin";
    
    echo exec("javac theNameOfYourJavaProgram.java 2>&1");//shows # of errors
    echo "<br />";
    echo exec("java theNameOfYourJavaProgram 2>&1");//this line executes it
    echo "<br />";
    echo shell_exec("javac theNameOfYourJavaProgram.java 2>&1 ");//compiles it 
    
    ?>
    
    0 讨论(0)
  • 2021-01-02 23:08

    I dont realy know, but i came a cross PHP-JAVA bridge maybe it can help

    http://php-java-bridge.sourceforge.net/pjb/

    Update:

    I tested this with Jasper Reports, and it is working really nice. It will allow you to Extend Java classes with PHP or just use Java class lik it was PHP.

    use java\lang\String as JString;
    require_once("javabridge/java/Java.inc");
    
    class String extends JString {
        function toString () {
            return "hello " . parent::toString();
        }
    }
    $str = new String("Java");
    echo $str->toString();
    

    or

    $temp = new Java('java.sql.Timestamp');
    $javaObject = $temp->valueOf('2007-12-31 0:0:0');
    
    
    $params = new Java("java.util.HashMap");
    $params->put("text", "This is a test string");
    $params->put("date",$javaObject);
    

    More examples: http://php-java-bridge.sourceforge.net/pjb/FAQ.html

    0 讨论(0)
提交回复
热议问题