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.
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.