- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
ClassNotFoundException when load script
Sat, 2010-05-15, 14:01
// ClassNotFoundException when load script
// for example, the following is test.scala file:
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();
/*-
When I load it from the interpreter ( :load test.scala ), the code will throw the exception:
java.lang.ClassNotFoundException: org.postgresql.Driver
But if I run it from the command line such as "scala test.scala", it will run without errors.
Why and how to do it right? I just don't want to load it as a script file, because I need to do something further interactively. I really hope I can benefit from scala console( groovy console maybe work, I didn't try, anyway it is too slow)
Actually, I have a program built upon spring, which runs correctly when executed as a script file( in the script I create a ClassPathXmlApplicationContext and then use it's components) but throws this exception when loaded interactively. I'm not sure whether spring and apache dbcp just have the same problems.
I use scala 2.7.7.final.
thanks.
*/
Tue, 2010-05-18, 08:37
#2
Re: ClassNotFoundException when load script
Hello, where did you put your driver class? Could you give us your project structure in detail?
On Tue, May 18, 2010 at 4:53 AM, ZhangQidi <zhangqidicn [at] yahoo [dot] com [dot] cn> wrote:
--
Sincerely yours and Best Regards,
Xie Xiaodong
On Tue, May 18, 2010 at 4:53 AM, ZhangQidi <zhangqidicn [at] yahoo [dot] com [dot] cn> wrote:
// ClassNotFoundException when load script
// for example, the following is test.scala file:
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();
/*-
When I load
it from the interpreter ( :load test.scala ), the code will throw the
exception:
java.lang.ClassNotFoundException:
org.postgresql.Driver
But if I run it from the command
line such as "scala test.scala", it will run without errors.
Why and how to do it right? I just don't want to load it as a script
file, because I need to do something further interactively. I really
hope I can benefit from scala console( groovy console maybe work, I
didn't try, anyway it is too slow)
Actually, I have a
program built upon spring, which runs correctly when executed as a
script file( in the script I create a ClassPathXmlApplicationContext and then use it's components) but throws this exception when loaded
interactively. I'm not sure whether spring and apache dbcp just have the same problems.
I use scala 2.7.7.final.
thanks.
*/
--
Sincerely yours and Best Regards,
Xie Xiaodong
Tue, 2010-05-18, 12:17
#3
Re: ClassNotFoundException when load script
I have an executable test_scala.bat file which looks like this:
---
set JAVA_OPTS=-Xmx1400m -Xms1400m
set mydir=d:\test
scala -encoding GBK -classpath %mydir%\lib\*;%mydir%\conf test.scala
--
test.scala file looks like this:
--
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();
--
And if I run test_scala.bat, it will run without any errors.
But if I run:
scala -encoding GBK -classpath d:\test\lib\*;d:\test\conf
(and then in scala console:)
:load test.scala
the ClassNotFoundException will be thrown out. But the message "ok,find me" shows that the class has been loaded successfully, so I think the classpath is not the problem.
(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...
---
set JAVA_OPTS=-Xmx1400m -Xms1400m
set mydir=d:\test
scala -encoding GBK -classpath %mydir%\lib\*;%mydir%\conf test.scala
--
test.scala file looks like this:
--
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();
--
And if I run test_scala.bat, it will run without any errors.
But if I run:
scala -encoding GBK -classpath d:\test\lib\*;d:\test\conf
(and then in scala console:)
:load test.scala
the ClassNotFoundException will be thrown out. But the message "ok,find me" shows that the class has been loaded successfully, so I think the classpath is not the problem.
(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...
// ClassNotFoundException when load script
// for example, the following is test.scala file:
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();
/*-
When I load
it from the interpreter ( :load test.scala ), the code will throw the
exception:
java.lang.ClassNotFoundException:
org.postgresql.Driver
But if I run it from the command
line such as "scala test.scala", it will run without errors.
Why and how to do it right? I just don't want to load it as a script
file, because I need to do something further interactively. I really
hope I can benefit from scala console( groovy console maybe work, I
didn't try, anyway it is too slow)
Actually, I have a
program built upon spring, which runs correctly when executed as a
script file( in the script I create a ClassPathXmlApplicationContext and then use it's components) but throws this exception when loaded
interactively. I'm not sure whether spring and apache dbcp just have the same problems.
I use scala 2.7.7.final.
thanks.
*/