Why doesn't jrunscript respect my classpath?

I am trying to make JDBC access using JavaScript using Rhino included in Java 6. But I can not get DriverManager find the Driver that I want to use.

These two examples should be equivalent:

Java:

 public class DbTest { public static void main(String[] argv) { java.sql.Connection c = null; try { java.lang.Class.forName("net.sourceforge.jtds.jdbc.Driver"); c = java.sql.DriverManager.getConnection( "jdbc:jtds:sqlserver://myserver/mydb", "user", "password"); } catch (Exception e) { c = null; System.out.println(e); }; if(c != null) { System.out.println("yay, got c!"); try { c.close(); } catch(Exception e) {} } else { System.out.println("awww."); } } } 

JavaScript:

 importPackage(Packages.net.sourceforge.jtds.jdbc); java.lang.Class.forName('net.sourceforge.jtds.jdbc.Driver'); var c = null; try { c = java.sql.DriverManager.getConnection( 'jdbc:jtds:sqlserver://myserver/mydb', 'user', 'password'); } catch (e) { c = null; println(e); }; if(c) { println('yay, got c!'); c.close(); } else { println('awww.'); } 

... but when I run them, I get the following:

Java:

 > java -cp .;jtds-1.2.5.jar DbTest java.sql.SQLException: Unknown server host name 'myserver'. awww. 

Great, he managed to load the driver and tried to resolve the server.

JavaScript:

 > jrunscript -cp .;jtds-1.2.5.jar dbtest.js script error in file dbtest.js : sun.org.mozilla.javascript.internal.WrappedException: Wrapped java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver (dbtest.js#2) in dbtest.js at line number 2 

Why doesn't he find a class? I tried with and without importPackage() and importClass() with and without the Packages prefix. If I comment forName , then DriverManager will not find a suitable driver.

+4
source share
1 answer

According to a post on the IBM DeveloperWorks forum , "The jrunscript -classpath value is used by a separate" scripting "class loader that is parallel to regular application class loaders and is used to resolve the classes mentioned in importClass () and importPackage ()."

And according to this SO response , "... DriverManager performs" tasks using the immediate calling instance of the class loader "".

So, if you do not put the jar driver in the bootclass path or cannot find a way to change the way jrunscript (and Ant <script /> ) is installed, install the system class loader from the script environment, the only way to get this to work seems to be to completely skip DriverManager :

 var c = null; try { var p = new java.util.Properties(); p.setProperty('user', 'user'); p.setProperty('password', 'password'); c = (new net.sourceforge.jtds.jdbc.Driver()).connect( 'jdbc:jtds:sqlserver://myserver/mydb', p); } catch (e) { c = null; println(e); }; if(c) { println('yay, got c!'); c.close(); } else { println('awww.'); } 

It removes one layer of indirection, which may or may not be a cup of tea, but it works (when installing a real server / user / passwd):

 $ jrunscript -cp jtds-1.2.5.jar dbtest_realparams.js yay, got c! 
+3
source

All Articles