Refinement of a Java prog called by PHP

D

Donkey Hot

system(java_prog_absolute_path/the_java_prog);

And the error returns from the java "slave" prog to the "master" php
prog is:


the error is:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at

Clearly the MySQLjdbc driver jar is not on the classpath.
 
R

Rose

After listening to everybody's replies, I rephrase my problems as follows:

I modify my program from my client:

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection ( "jdbc:mysql://127.0.0.1/" +
dbstr, "root","");

....

after a series of search in the db "dbstr",

I want to return result from the search obtained by this Java program by
simply using System.out.printIn because I tried a C program and printf
simply works.

In order to make sure the java program can be called by PHP successfully, i
place it under the php directory instead of calling

system(java_prog_absolute_path/the_java_prog);

And the error returns from the java "slave" prog to the "master" php prog
is:


the error is:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at
java.net.URLClassLoader$1.run(URLClassLoader.java:200) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:188) at
java.lang.ClassLoader.loadClass(ClassLoader.java:306) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276) at
java.lang.ClassLoader.loadClass(ClassLoader.java:251) at
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) at
java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Class.java:169) at
SearchDatabase4.(SearchDatabase4.java:74) at
SearchDatabase4.main(SearchDatabase4.java:344)

And the file SearchDatabase4.java is:
74: Class.forName("com.mysql.jdbc.Driver");
344: new SearchDatabase4(args[0], Double.parseDouble(args[1]),
 
R

Rose

Donkey Hot said:
Clearly the MySQLjdbc driver jar is not on the classpath.

But I have also copied that ( mysql_jdbc.jar) to the same directory and
running the java prog in a tcsh shell is fine, only running it through PHP
gets the problem
 
D

Donkey Hot

But I have also copied that ( mysql_jdbc.jar) to the same directory
and running the java prog in a tcsh shell is fine, only running it
through PHP gets the problem

Try starting the java prog while being NOT in the same directory, while in
tcsh shell. Does it still work? PHP propable does not have the current
directory there, while it calls the java-app with absolute path.

What does

mean? Shouldnt it be

system("java java_prog_absolute_path/the_java_prog")

or

system("java -classpath
java_prog_absolute_path;mysql_jar_absolute_path/mysql_jdbc.jar
the_java_prog")

Note the ";" in classpath.. for Windows. Have to be ":" for *nix
 
L

Lew

Rose said:
After listening to everybody's replies, I rephrase my problems as follows:

I modify my program from my client:

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection ( "jdbc:mysql://127.0.0.1/" +
dbstr, "root","");

Side note: Once you have the MySQL driver in your classpath, you only need to
load the class once. Loading the class repeatedly just has the class loader
look the second and subsequent times and go, "Yep, it's here!"
 
R

Rose

Lew said:
Side note: Once you have the MySQL driver in your classpath, you only
need to load the class once. Loading the class repeatedly just has the
class loader look the second and subsequent times and go, "Yep, it's
here!"

Thank you for both Lew and Donkey Hot. The java program, originally tested
at /tmp and works fine. Then, as a newbie, i thought i can simply tell PHP
to call the program and get the result as it works in a C-compiled program.
It doesn't. So I move the whole bunch of executables and relevant files
under the PHP directory but it still doesn't work. Finally I followed Donkey
Hot's advice by adding "-cp .:./mysql_jdbc.jar" and it works now. Indeed, I
don't know what the arguments are for, neither do I understand Lew's
comments on "driver, <-- do you mean the jar? indeed it is a file given to
me, i don't know where it comes from", "load the class once <-- how to tell
the prog to load it only once in a web environment?"

Thanks again. At least it now works although I don't quite know why. ;)
 
L

Lew

Rose said:
Finally I followed Donkey
Hot's advice by adding "-cp .:./mysql_jdbc.jar" and it works now. Indeed, I
don't know what the arguments are for, neither do I understand Lew's
comments on "driver, <-- do you mean the jar? indeed it is a file given to
me, i don't know where it comes from", "load the class once <-- how to tell
the prog to load it only once in a web environment?"

Excellent question. The 'Class.forName()' expression is what loads the
driver. Class loading is one of the Dark Arts. Suffice to say that, for any
given run of the JVM, you usually only have to refer to a class once to get
its magical class loader to load the definition of the class in memory.

In the case of JDBC drivers there's more than a little chicanery involved.
JDBC drivers have a set of secret promises to keep, which they do when they
are loaded into the JVM. Class.forName() forces a load of the named class,
which then goes about keeping all its secret promises inside its static
initializer. Its main job is to register itself with the DriverManager, but
it conveniently hides that fact from you, the programmer.

Once that is done, every subsequent call to Class.forName() for the same
driver does no further good. It doesn't really do any harm, either, except
spin a few cycles uselessly checking. The best way to handle it is to include
it in the static initializer of your own custom data-access base class, or the
init() of a servlet, or some such block of code that you can guarantee to run
before an actual database call.

I called this small matter a "side note" because it is not the most critical
issue that data-access code faces, but it is part of a larger "initialize,
prepare, execute, clean up" gestalt.
 
N

Nigel Wade

Rose said:
After listening to everybody's replies, I rephrase my problems as follows:

I modify my program from my client:

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection ( "jdbc:mysql://127.0.0.1/" +
dbstr, "root","");

...

after a series of search in the db "dbstr",

I want to return result from the search obtained by this Java program by
simply using System.out.printIn because I tried a C program and printf
simply works.

In order to make sure the java program can be called by PHP successfully, i
place it under the php directory instead of calling

system(java_prog_absolute_path/the_java_prog);

And the error returns from the java "slave" prog to the "master" php prog
is:

Besides the other helpful advice you received, have you considered that PHP has
a MySQL API of its own? You might be able to code the application entirely in
PHP and avoid the overhead of initiating both the PHP interpreter and the Java
JVM, and the complication of reading the output from the Java code into PHP.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top