class not found error while using jtds in java
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开发者_C百科 ?
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)
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
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!).
精彩评论