控制台抛出ClassNotFoundException,请教如何解决

zhangqidi 2010-05-20
在scala-user上没人回答,来这里问问吧:

我写了个批处理文件test_scala.bat启动scala控制台,大概是这样的(c:\test\lib下面存放了相关jar):

---
set JAVA_OPTS=-Xmx1400m -Xms1400m
scala -classpath .;.\lib\* test.scala
--

一个test.scala脚本文件大概是这样的:

--

import org.apache.commons.dbcp.BasicDataSource;

import javax.sql.DataSource;

val cn = "org.postgresql.Driver"
val cloader = Class.forName("org.postgresql.Driver").getClassLoader()
Class.forName(cn).newInstance()

println("ok, you find me in classpath", cloader.loadClass(cn))

val dds: BasicDataSource = new BasicDataSource();
dds.setDriverClassName("org.postgresql.Driver");
dds.setUrl("jdbc:postgresql://localhost/test");
// 仅仅测试,不需要本地安装数据库
dds.setUsername("postgres");
dds.setPassword("111111");

dds.getConnection().close();

--

如果我直接运行test_scala.bat,不会有任何错误.一切顺利.

但是如果我改变一下方式,改为交互式运行:

scala -encoding GBK -classpath c:\test\lib\*

(然后在控制台中加载脚本)
:load test.scala

那么将首先打印"ok,find me", 然后抛出ClassNotFoundException.因为jar包在lib下面,而且显示也找到了,所以应该不是classpath问题.难道是AccessController导致的问题?

因为我需要利用控制台的交互功能,希望首先加载数据,然后进一步做一些查询和数据处理,而数据加载时间较长,所以只能用控制台交互的方式.不知道这个问题如何解决? 谢谢!

我用的scala 2.7.7.final


----------------
(ok,find me,class org.postgresql.Driver)
java.lang.ClassNotFoundException: org.postgresql.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoa...good





zhangqidi 2010-05-20

不能上传附件,郁闷

 

 

这是控制台输出

 

 

C:\test>tscala.bat

C:\test>set JAVA_OPTS=-Xmx1400m -Xms1400m

C:\test>scala -classpath .;.\lib\* test.scala
(ok, you find me in classpath,class org.postgresql.Driver)

C:\test>tscala2.bat

C:\test>set JAVA_OPTS=-Xmx1400m -Xms1400m

C:\test>scala -classpath .;.\lib\*
Welcome to Scala version 2.7.7.final (Java HotSpot(TM) Client VM, Java 1.6.0_18).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :load test.scala
Loading test.scala...
import org.apache.commons.dbcp.BasicDataSource
import javax.sql.DataSource
cn: java.lang.String = org.postgresql.Driver
cloader: java.lang.ClassLoader = java.net.URLClassLoader@d7c6bf
res0: Any = org.postgresql.Driver@169da74
(ok, you find me in classpath,class org.postgresql.Driver)
dds: org.apache.commons.dbcp.BasicDataSource = org.apache.commons.dbcp.BasicDataSource@14828e7
java.lang.ClassNotFoundException: org.postgresql.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoa...
scala>

 

 

 

这是仅仅需要的几个文件:

 

C:\test>dir /s
 驱动器 C 中的卷是 System
 卷的序列号是 7841-0C21

 C:\test 的目录

2010-05-20  00:27    <DIR>          .
2010-05-20  00:27    <DIR>          ..
2010-05-20  00:17    <DIR>          lib
2010-05-20  00:29             1,325 output.txt
2010-05-20  00:29             1,552 test.scala
2010-05-20  00:28                71 tscala.bat
2010-05-20  00:28                60 tscala2.bat
               4 个文件          3,008 字节

 C:\test\lib 的目录

2010-05-20  00:17    <DIR>          .
2010-05-20  00:17    <DIR>          ..
2010-05-16  14:08           121,757 commons-dbcp.jar
2010-05-16  14:08            62,103 commons-pool.jar
2010-05-16  14:08         1,064,270 javaee.jar
2010-05-16  14:08           431,283 postgresql.jdbc3.jar
               4 个文件      1,679,413 字节

     所列文件总数:
               8 个文件      1,682,421 字节
               5 个目录  1,658,937,344 可用字节

C:\test>

 

 

 

 

zhangqidi 2010-05-20
郁闷,到2.8.0.RC2上测试了一下,却没有问题:

C:\test>tscala.bat

C:\test>set JAVA_OPTS=-Xmx1400m -Xms1400m

C:\test>E:\scala-2.8.0.RC2\bin\scala -classpath .;.\lib\* test.scala
(ok, you find me in classpath,class org.postgresql.Driver)

C:\test>tscala2.bat

C:\test>set JAVA_OPTS=-Xmx1400m -Xms1400m

C:\test>E:\scala-2.8.0.RC2\bin\scala -classpath .;.\lib\*
Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) Client VM, Java 1.6.0_18).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :load test.scala
Loading test.scala...
import org.apache.commons.dbcp.BasicDataSource
import javax.sql.DataSource
cn: java.lang.String = org.postgresql.Driver
cloader: java.lang.ClassLoader =
URLClassLoader(
  file:/C:/Program%20Files/Java/jdk1.6.0_18/jre/lib/resources.jar
  file:/C:/Program%20Files/Java/jdk1.6.0_18/jre/lib/rt.jar
  file:/C:/Program%20Files/Java/jdk1.6.0_18/jre/lib/jsse.jar
  file:/C:/Program%20Files/Java/jdk1.6.0_18/jre/lib/jce.jar
  file:/C:/Program%20Files/Java/jdk1.6.0_18/jre/lib/charsets.jar
  file:/C:/Program%20Files/Java/jdk1.6.0_18/jre/lib/ext/dnsns.jar
  file:/C:/Program%20Files/Java/jdk1.6.0_18/jre/lib/ext/localedata.jar
  file:/C:/Program%20Files/Java/jdk1.6.0_18/jre/lib/ext/sunjce_provider.jar
  file:/C:/Program%20Files/Java/jdk1.6.0_18/jre/lib/ext/sunmscapi.jar
  file:/C:/Program%20Files/Java/jdk1.6.0_18/jre/lib/ext/sunpkcs11.jar
  file:/E:/SCALA-~1.RC2/bin/../lib/jline.jar
  file:/E:/SCALA-~1.RC2/bin/../lib/sca...res0: Any = org.postgresql.Driver@f757b9
(ok, you find me in classpath,class org.postgresql.Driver)
dds: org.apache.commons.dbcp.BasicDataSource = org.apache.commons.dbcp.BasicDataSource@1bcfbeb

scala>
greatwqs 2013-01-31
也出现灵异的事件了
Global site tag (gtag.js) - Google Analytics