Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

php

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 the file, but I cannot get a java program to be successfully run from within the php script.
I have tried exec(), shell_exec(), and system() with no results.
For the command, I have used "java Test", "java < directory >/Test", "/usr/bin/java < directory >/Test", I have even set up the application as a jar file with no results. The actual line of code I have used is:

echo shell_exec("java Test");

Usually there is no output. However, if I have just shell_exec("java"), then the last line of the help from java ("show splash screen with specified image") is displayed, which shows that the command has been executed. If I use, for example, shell_exec("whoami") I get "nobody" returned, which is correct. The only thing the java file does is create a file so that I can see that the application has been successfully run (the application runs successfully if I run it on the command line). I have set the permissions for the java file to 777 to rule out any possibility of permission errors. 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?

Thanks.

like image 225
user450775 Avatar asked Sep 17 '10 16:09

user450775


People also ask

Can we run Java code in PHP?

The PHP exec() function is the way to go, but you should be very careful in what you allow to executed.. in other words don't rely on user input as it could potentially compromise your entire server. Calling the Java application launcher using exec, you can execute any Java application from PHP, e.g.

How do I execute a Java program?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.


Video Answer


1 Answers

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.
like image 153
Alfred Avatar answered Sep 20 '22 17:09

Alfred