Refinement of a Java prog called by PHP  
Author Message
Rose





PostPosted: 2008-2-3 21:30:00 Top

java-programmer, Refinement of a Java prog called by PHP 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]),


 
Rose





PostPosted: 2008-2-3 22:11:00 Top

java-programmer >> Refinement of a Java prog called by PHP
"Donkey Hot" <email***@***.com> wrote in message
news:email***@***.com...
> "Rose" <email***@***.com> wrote in
> news:fo4flg$s4v$email***@***.com:
>
>> 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.
>

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


 
Donkey Hot





PostPosted: 2008-2-3 22:16:00 Top

java-programmer >> Refinement of a Java prog called by PHP "Rose" <email***@***.com> wrote in
news:fo4i0q$t50$email***@***.com:

> "Donkey Hot" <email***@***.com> wrote in message
> news:email***@***.com...
>> "Rose" <email***@***.com> wrote in
>> news:fo4flg$s4v$email***@***.com:
>>
>>> 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.
>>
>
> 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

>>> system(java_prog_absolute_path/the_java_prog);

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

 
 
Lew





PostPosted: 2008-2-3 23:07:00 Top

java-programmer >> Refinement of a Java prog called by PHP Rose wrote:
> 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!"

--
Lew
 
 
Rose





PostPosted: 2008-2-3 23:38:00 Top

java-programmer >> Refinement of a Java prog called by PHP
"Lew" <email***@***.com> wrote in message
news:email***@***.com...
> Rose wrote:
>> 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!"
>
> --
> Lew

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



 
 
Nigel Wade





PostPosted: 2008-2-4 18:32:00 Top

java-programmer >> Refinement of a Java prog called by PHP Rose wrote:

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

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : email***@***.com
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555