java - Why doesn't jrunscript honor my classpath? -
I am trying to access some jdbc using rhino included in java669. But I should be the same as two examples:
Java:
public domain DbTest {public static zero 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"); } Hold (exception e) {c = null; Println (E); }; If (c! = Null) {System.out.println ("yay, got c!"); Try {c.close (); } Hold (exception e) {}} and {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'); } Hold (e) {c = null; Println (e); }; If (c) {println ('yay, got c!'); C.close (); } Other {println ('awww.'); }
... But when I drive them, I get this behavior:
Java:
& gt; Java-CP .; Jtds-1.2.5.jar DBTVWork Java SQL Cleopation: Unknown server host name 'MySaser' Awww
This is great, it managed to load the driver and tried to solve the server.
Javascript:
& gt; Jrunscript -cp .; Jtds-1.2.5.jar dbtest.js file error in dbtest.js: sun.org.mozilla.javascript.internal.WrappedException: wrapped java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc line number 2 dbtest.js In Driver (dbtest.js # 2)
Why does not this class search? I have tried without the prefix and without the importPackage ()
and importClass ()
and packages
. If I comment on forName
, then DriverManager
does not find the appropriate driver. According to
, "jrunscript -classpath value is used by a different" scripting "classloader The normal application is the same as the classloader and used to solve those classes which are described in the import class () and the import package () ".
and "Class Loader Examples of Immediate Collars" "by using" ... DriverManager performs ".
So, Do not put the driver jar in the boot class or find a way to modify jrunscript
(and ant
System Set Script environment classloader , The only way to do this work is to leave DriverManager
completely:
var c = null; try {var p = new java.util.Properties (); P.setProperty ('Users', 'User'); P.setProperty ('password', 'password'); c = (new net.sourceforge.jtds.jdbc.Driver ()). Connect ('jdbc: jtds: sqlserver: // myserver / mydb' , P);} hold (e) {c = null; println (e);}; if (c) {println ('yay, got c!'); C.close ();} other {println ('awww . ');}
This removes a layer of indirection, which can not be tea tea or not, but it works (actual server / user / passwd included Is):
$ jrunscript -cp jtds-1.2.5.jar dbtest_realparams.js Yay, got it!