![]() |
|
|
|||||||
![]() |
Java - Refinement of a Java prog called by PHP |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
"Rose" <> wrote in
news:fo4flg$s4v$: > 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. Donkey Hot |
|
|
|
|
#2 |
|
Posts: n/a
|
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.j ava:18 java.lang.ClassLoader.loadClass(ClassLoader.java:3 06) at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276) at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51) at java.lang.ClassLoader.loadClassInternal(ClassLoade r.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 |
|
|
|
#3 |
|
Posts: n/a
|
"Donkey Hot" <> wrote in message news:Xns9A399EE6961C6SH15SGybs1ysmajw54s5@194.100. 2.89... > "Rose" <> wrote in > news:fo4flg$s4v$: > >> 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 Rose |
|
|
|
#4 |
|
Posts: n/a
|
"Rose" <> wrote in
news:fo4i0q$t50$: > "Donkey Hot" <> wrote in message > news:Xns9A399EE6961C6SH15SGybs1ysmajw54s5@194.100. 2.89... >> "Rose" <> wrote in >> news:fo4flg$s4v$: >> >>> 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 Donkey Hot |
|
|
|
#5 |
|
Posts: n/a
|
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 Lew |
|
|
|
#6 |
|
Posts: n/a
|
"Lew" <> wrote in message news:. .. > 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. Rose |
|
|
|
#7 |
|
Posts: n/a
|
Rose wrote:
> 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. -- Lew Lew |
|
|
|
#8 |
|
Posts: n/a
|
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 : Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555 Nigel Wade |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Logging html page id into php | brentcole | General Help Related Topics | 0 | 12-28-2008 01:53 PM |
| Passing value with out using variable in query string in PHP! | Ali_ggl | General Help Related Topics | 0 | 11-29-2008 12:22 PM |
| how to email URL using php? | snic07 | General Help Related Topics | 2 | 05-05-2008 07:48 AM |
| About PHP...................... | smashing_blizzard | General Help Related Topics | 0 | 10-02-2007 08:16 PM |
| New sci fi film on the block called THE FINAL EQUATION | dan_abella@hotmail.com | DVD Video | 0 | 09-30-2007 08:18 PM |