Search code examples
javasqlsql-server-2008jarjtds

Exporting jTDS and my project in a single JAR?


I was trying to make my application work with jTDS (MS SQL Server 2008). So I did the following:

  • downloaded the latest jTDS driver (1.3.0)
  • pasted the driver into the project folder
  • configured the build path in Eclipse by adding the jar and
  • in the Order and Export tab, I selected all entries

I then exported the jar. As it is not an runnable jar, I afterwards tested it and it can't seem to find my driver (java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver).

When I test it in Eclipse by executing only something this:

public static void main(String[] args) {}
    String classForName = "net.sourceforge.jtds.jdbc.Driver";

    try
    {
        // embedding driver for sql connection
        Class.forName(classForName);

        con = java.sql.DriverManager.getConnection(getConnectionUrl(), user, pass);
        if (con != null)
        {
            JOptionPane.showMessageDialog(null, "Connection Successful!");
        }

    }
    catch (Exception e)
    {
        Log.writeStackTrace(e);
    }
}

.. it then works like a charm. So in my thoughts, the problem seems to in including my jTDS jar. Did I miss something?


Solution

  • Damn, I'm just slightly too late!

    The standard Java classloaders don't support nesting JARs within JARs. You can either combine your JARs together (as you've done!), or you can use a more sophisticated classloader with something like One-Jar (http://one-jar.sourceforge.net/).