为什么 jrunscript 不尊重我的类路径?

发布于 09-12 00:36 字数 1968 浏览 22 评论 0原文

我正在尝试使用 Java 6 中包含的 Rhino 从 JavaScript 进行一些 JDBC 访问。但是我无法使 DriverManager 找到我想要使用的 Driver

这两个示例应该是等效的:

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

... 但是当我运行它们时,我得到以下行为:

Java:

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

太棒了,它设法加载驱动程序并尝试解析服务器。

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

为什么找不到该类?我尝试过使用和不使用 importPackage()importClass(),使用和不使用 Packages 前缀。如果我注释掉 forName,则 DriverManager 找不到合适的驱动程序。

I'm trying to do some JDBC access from JavaScript using the Rhino included in Java 6. But I cannot make the DriverManager find the Driver 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 this behaviour:

Java:

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

That's great, it 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 it find the class? I have tried with and without importPackage() and importClass(), with and without the Packages prefix. If I comment out forName, then DriverManager doesn't find a suitable driver.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月棠2024-09-19 00:36:44

根据 IBM DeveloperWorks 论坛帖子,“jrunscript -classpath 值由单独的“脚本”类加载器使用,该类加载器与通常的应用程序类加载器并行,用于解析 importClass() 中提到的类和 importPackage()”。

并根据 这个 SO 答案,“... DriverManager 执行“使用直接调用者的类加载器实例的任务” ”。

因此,除非您将驱动程序 jar 放入 bootclasspath 或找到一种方法来修改 jrunscript (和 Ant

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

它删除了一层间接,这可能是也可能不是一杯茶,但它可以工作(与真实的服务器/用户/密码插入):

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

According to an IBM DeveloperWorks forum post, "the jrunscript -classpath value is used by a separate "scripting" classloader that parallels the usual application classloader and that is used to resolve classes that have been mentioned in importClass() and importPackage()".

And according to this SO answer, "... DriverManager performs "tasks using the immediate caller's class loader instance" ".

So, unless you put the driver jar into the bootclasspath or find a way to modify how jrunscript (and Ant <script />) set the system classloader of the script environment, the only way to get this to work seems to be to skip DriverManager entirely:

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 ones cup of tea, but it works (with real server/user/passwd inserted):

$ jrunscript -cp jtds-1.2.5.jar dbtest_realparams.js 
yay, got c!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文