8

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.

3
  • Saw this just the other day: stackoverflow.com/search?q=call+java+script+PHP Commented Sep 17, 2010 at 16:11
  • Is your java class in a package? If so, you need to specify the package, and your directory structure should reflect the package. Put another way, if you're at a command line in the same directory, can you run the java class manually using the same line? Commented Sep 17, 2010 at 16:15
  • No, the java class is not in a package, and I have tested the command on the command line and it runs successfully there. Commented Sep 17, 2010 at 16:22

4 Answers 4

6

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.
Sign up to request clarification or add additional context in comments.

1 Comment

+1 For a permissions problem on the folder you're writing the file to (ie. check that user nobody has write access to the folder you want to write the file to).
3

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

Comments

1

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.

1 Comment

I thought of that and tried to mention this in my post by using "/usr/bin/java < directory >/Test", however when the question was posted, "< directory >" was removed and just "/Test" remained. Sorry about that.
0
<?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 

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.