2

I have a PHP page that processes incoming information, it takes the parameters and constructs a command to execute a Java class on the server using shell_exec().

The Java class takes the parameter and calls a stored procedure in MySQL and returns the required results. i have tested my Java class independently from the terminal and it is working perfectly.

My problem is that when I execute the Java class from PHP it runs until the line where I am defining the stored procedure calls and it stops there and returns NULL!

cSt = conn.prepareCall("{CALL prcName(?,?,?,?)}");

The are no exceptions while executing since the above line is in a try catch and no message from catch is printed.

The program just runs until this line and returns NULL!

Here is the PHP page:

<?php     
    $name= $_POST['name'];
    $contact= $_POST['contact'];

    $response =  shell_exec("java ClassName".$name." ".$contact);

    echo $response;
?>

Java class (part):

public static void main(String[] args) {
        //connection to database
        int returnValue = methodName(args[0], args[1]);
        System.out.print(String.valueOf(returnValue));
        //close connection to db
}
public static int methodName(String name,String contact){
        int result = 5; 
        try {
            cSt = conn.prepareCall("{CALL prcName(?,?,?)}");
            cst.CallableStament().setString("name",name);
            cst.CallableStament().setString("contact",contact);
            cst.CallableStament().registerOutParameter("rtn", java.sql.Types.INTEGER);
            cst.CallableStament().execute();

            result = cst.CallableStament().getInt("rtn");           
        } catch (SQLException e) {e.printStackTrace();}
        return result;
    }

UPDATE: i have narrowed down from where the problem was and finally found that while specifiying the driver by Class.forName("com.mysql.jdbc.Driver"); i get the error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:195)
    at Classname.Connect(classname.java:29)
    at classname.

UPDATE 2: to be able to use the driver i added the CLASSPATH in /etc/profile/ however i noticed that the change is only for login users. But apache is using the user "www-data" while running the shell_exec() and i cant figure a way to assign the classpath to that user.

9
  • 1
    add e.printStacktrace() to get the details of the error. Commented Nov 7, 2015 at 18:18
  • show more code T. mysqltutorial.org/calling-mysql-stored-procedures-from-jdbc Commented Nov 7, 2015 at 18:20
  • There are no errors! the java program just stop at that line when i call it using shell_exec(). i used system.out.print() in the method calling the stored procedure to know where exactly stops working. Commented Nov 7, 2015 at 18:22
  • could you post the relevant code Commented Nov 7, 2015 at 18:24
  • added the code close to what i have! Commented Nov 7, 2015 at 18:33

1 Answer 1

1

You have to add the classpath to the executed command, or else set CLASSPATH before you run the PHP script.

$response =  shell_exec("java -classpath mysql.jar ClassName".$name." ".$contact);

java -h

-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
              A ; separated list of directories, JAR archives, # : separated list on Linux
              and ZIP archives to search for class files.

Oh, by the way, executing a shell command based on the contents of a web POST is super insecure. Someone could just POST ; rm -f / to wipe out your system, or other things to hack you.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the info. but is there any other way to execute those java classes from php which shell command?

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.