简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM