2

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)
3
  • which class it doesn't find ? Commented Jan 21, 2011 at 8:05
  • What class isn't being found? Please print out the full exception. Commented Jan 21, 2011 at 8:06
  • it doesn't find net.sourceforge.jtds.jdbc.Driver class . Added the full exception to the question Commented Jan 21, 2011 at 8:07

1 Answer 1

2

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!).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.