2

My website needs PHP to run a Java program in the background. So, PHP issues exec() method to let Java do all the work. The implementation works fine on Windows, but completely failed on Ubuntu. While exec() doesn't work, a stand-alone test with console works just fine.

I've setup a test.php to narrow down the problem:

<?php
$output = exec("java -cp ~/path/to/java/class/file/folder Hello 2>&1");
//$output = exec("whoami");
echo $output;
?>


The Hello.java is simply:

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


By running test.php on localhost, it shows:

Error: Could not find or load main class Hello


I tried to narrow down the cause of the error, and my thought went like this:

  1. exec() itself is problematic:

    unlikely, since whoami prints out apache-user as expected.

  2. what the error message means:

    I searched about this error. Post like this one talks about it is caused by the absence of classpath. It's not the case for me either, because in console it works. So the error message means nothing (does it?)

  3. user/group permission:

    Is it possible that apache-user is not permitted to run the class file?
    I checked, and found the permission code of Hello.class to be rw-r--r--, owned by apache-user:webmasters.
    But, even if no one has x permission of the file, in console I can still run it (using my own user).
    I'm not sure about the situation here. But my understanding is that by running java program, it is really JVM executing it (or something else); so the permission of Hello.class doesn't matter.


    I found another post has a similar situation. But its solution - specifying full path to Java bin /usr/bin/java - doesn't work for me...


What is causing the error?

Can anyone help? Detailed solution is appreciated! I'm a newbie @_@

Many thanks!!!

Community
  • 1
  • 1
Leo
  • 311
  • 2
  • 11
  • 1
    Is the path/to/java file accessible by the apache-user? – glarkou Dec 16 '12 at 09:38
  • Yes, apache-user is the owner of both the file and the subdirectory, and in the group to the project base dir, which has `r` access at least. – Leo Dec 16 '12 at 09:42
  • wait... `ls /path/to/java/class/foldr 2>&1' access denied. Let me try again... My bad! – Leo Dec 16 '12 at 09:47

3 Answers3

0

Have you tried java -cp /path/to/folder/containing/class/file Hello 2>&1? It appears that the class file itself should not be the classpath. It should be in the classpath. If this were a .jar file, on the other hand, then you would provide the filename in the classpath.

Soumya
  • 13,677
  • 6
  • 34
  • 49
  • Hi Soumya! My bad. In the real path name, it is the directory, not the class file itself. I've edited my question to reflect that. Sorry for the confusion! – Leo Dec 16 '12 at 10:07
0

Give the path and Hello.java file free.

Test the rights for the apache user with:

sudo -u webmasters java -cp /path/to/java/class/file/folder Hello

chmod a+r Hello.class
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

I've solved the problem... It's quite unexpected.

I changed the classpath.

Previously it's something like ~/myproject/to/java/class/file/folder. And I changed it to /home/myuser/myproject/to/java/class/file/folder.

But I completely don't understand why ~ notation doesn't work with exec().

Leo
  • 311
  • 2
  • 11
  • 2
    ~ Is referring the current users home directory. So when running the script under apache the current user is in your case apache-user. So when you execute php exec() using the ~ it would be most likely pointing to /home/apache-user/. Which from your own answer is not were the script is located. – PhearOfRayne Dec 16 '12 at 10:33
  • 1
    `~` doesn't work because it's a _"placeholder"_ for the home directory of the current user. 1 of 3 things is wrong: the home directory of apache user isn't the one you want. 2: you're running in safe mode, and the `~` is being escaped (not sure if PHP escapes tildes, but could be) _or_ you're relying on a given .profile file to be loaded, which `exec` doesn't do either (probably irrelevant here) – Elias Van Ootegem Dec 16 '12 at 10:36
  • Ok, I get it. Thanks guys! – Leo Dec 17 '12 at 07:35