Problem to launch a program with system()


I wrote a JAVA program which uses the JAVA API JDOM 1.0 (of this site
I put the archive jdom.jar in the directory /usr/share/java/jdom.jar
and I added this path in the CLASSPATH variable.
The program runs normally when I launch it in a unix shell.

Now, I want to launch this JAVA program in a PHP program with system()
function for example. I have an Apache server with PHP 4 who runs. The
variable safe_mode of PHP is at off. So, the program PHP can launch
executable program.

But, It doesn’t because of CLASSPATH problem I think. So, to understand
where is the problem I recuced my programs and this is the 2 programs :

– program JAVA :

import org.jdom.*;
import org.jdom.input.*;

class TestSAX{



void instanciateSAXBuilder()
//On cr=E9e une instance de SAXBuilder
SAXBuilder sxb =3D new SAXBuilder();

System.out.println(“Constructeur de SAX”);

}catch(Throwable e){



public class Test {
public static void main (String[] args) {

PrintStream ps =3D null;


ps =3D new PrintStream(“./test.txt”);

}catch(Exception e){




try {
System.out.println(“Step 1”);
TestSAX ts =3D new TestSAX();
System.out.println(“Step 2”);
System.out.println(“Step 3”);
} catch (Throwable t) {



– program PHP


$line =3D system(“/usr/bin/java -cp /usr/share/java/jdom.jar Test”,
echo”Statut : “.$retval;

? >

On my computer, Apache runs on the user www-data. And i tried to
execute my program JAVA on this user and it runs normally. Besides, the
CLASSPATH of the user www-data contain the path to jdom.jar

This is the error message (contained in the file test.txt) after the
execution of the PHP program :

Step 1
java.lang.NoClassDefFoundError: org/jdom/input/SAXBuilder
at Test.main(

So, it seems that the program doesn’t find the SAXBuilder class what it
wants to tell that the CLASSPATH is not good when PHP launch the
program JAVA. But, I put the option -cp by launching the JAVA program
in the PHP program so the CLASSPATH put in this option should be good.

To be sure that this option could run, I made the following PHP program

$line =3D system(“/usr/bin/javac”, $retval);

After that, it doesn’t compile the program. So, I tried that :

$line =3D system(“/usr/bin/javac -cp /usr/share/java/jdom.jar”,

And there, the program is normally compiled !
So, it’s strange that here the CLASSPATH runs and in the other example
it doesn’t run.

Somebody will have and idea about modifications that I can do on the
programs ?

Thanks to help me.


Leave a Reply

Your email address will not be published. Required fields are marked *