在java中使用jtds时找不到类错误
我正在尝试使用 jtds jdbc 驱动程序 连接到 sql 服务器。以下是代码
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
this.conn = DriverManager.getConnection(
connectString, username, password);
System.out.println("connected");
}
catch (Exception e) {
e.printStackTrace();
}
包编译,但是当我尝试在终端中使用以下命令运行它时,
java -cp .:jtds-1.2.5.jar org.kodeplay.migration.TestConnection
它仍然抛出 ClassNotFoundException。所以这似乎不是正确的方法。如何做到这一点?
我已将 jtds-1.2.5.jar 文件复制到类路径目录中,该目录是 在 ubuntu 桌面上的 /etc/environment 文件中指定
编辑:它打印的完整异常如下
java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at org.kodeplay.migration.SqlDb.connect(SqlDb.java:21)
at org.kodeplay.migration.TestConnection.main(TestConnection.java:13)
I am trying to connect to a sql server using jtds jdbc driver. Following is the code
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
this.conn = DriverManager.getConnection(
connectString, username, password);
System.out.println("connected");
}
catch (Exception e) {
e.printStackTrace();
}
The package compiles but when I try to run it using following command in terminal
java -cp .:jtds-1.2.5.jar org.kodeplay.migration.TestConnection
it still throws a ClassNotFoundException. So this doesnt seem to be the right way. How to do this ?
I have copied the jtds-1.2.5.jar file in the classpath directory which is
specified in the /etc/environment file on a ubuntu desktop
Edit : full exception that it prints is as follows
java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at org.kodeplay.migration.SqlDb.connect(SqlDb.java:21)
at org.kodeplay.migration.TestConnection.main(TestConnection.java:13)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么?在命令行上指定在当前目录中查找该文件。
仅当您没有在命令行上手动指定类路径时,才会使用任何环境变量(在
/etc/environment
或其他任何地方设置)(当您未在命令行上手动指定类路径时,它们也会被忽略)你执行一个 jar 文件!)。Why? On the command-line you specify to look for that file in the current directory.
Any environment variables (set in
/etc/environment
or anywhere else) are only used when you don't specify the classpath manually on the command line (they are also ignored when you execute a jar file!).