I am attempting to deploy a swing application that uses a dll to facilitate a database connection (using sqljdbc4 jar that depends on sqljdbc_auth.dll). My deployment strategy is creating an executable jar file. The problem is that I cannot get the functions that rely on the sqljdbc_auth.dll to work in the executable jar I create.
I can get the project working fine in eclipse by using any of the following methods:
The 4th method I have tried is to load the dll directly in code via the following line. System.load("C:/Users/Me/Desktop/sqljdbc_auth.dll");
I have been unsuccessful with this approach and get the following error.
Mar 20, 2015 4:18:44 PM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
However, that error doesn't come directly from the line of code that loads the library, it seems to come afterwards when the sqljdbc library is trying to load the dll for use in making the connection.
Either way, I cannot get anything of the above methods working when I deploy the app as an executable jar file via the export functionality of eclipse. The only thing I can get is the previously mentioned error message.
The following sample application produces the same result.
public class TestApp {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
JFrame mainFrame = new JFrame();
mainFrame.setSize(300,300);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
try{
//System.load("C:/Users/Me/Desktop/sqljdbc_auth.dll");
String serverName = "local";
String databaseName = "test_database";
String connString = "jdbc:sqlserver://" + serverName + "; databaseName=" + databaseName + ";integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(connString);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select distinct name from test_table");
rs.next();
JOptionPane.showMessageDialog(null, "From db: " + rs.getString(1), "Test", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.getMessage(), "Test", JOptionPane.INFORMATION_MESSAGE);
} catch (ClassNotFoundException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.getMessage(), "Test", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.getMessage(), "Test", JOptionPane.INFORMATION_MESSAGE);
}
}
});
}
}
I've read tons of posts on using dlls with executable jars and they all seem to reference using this System.load method. Ironically, that is the one thing I can't get working. I know I have the right dll; however, because I can get other methods to work in the IDE environment. I don't care if the dll is packaged with the executable jar or not, I just want it to work!
I would presume this has something to do with putting the dll's in your library
(In Eclipse, Properties->Java Build Path->Libraries).
When you export the jar, you also will have the option of exporting the library files into a folder.
If you decompile the Jar after that, you'll notice that there's a manifest file, and in it, paths to where your library files are (based on the export, which created a library folder for you, generally jarname_lib).
When you export, you have the option to save as ANT file, which you can then edit to change the export location of the library files to a folder name of your choice. You can then add this edited ANT file to your build so that it happens whenever you build the project:
(In Eclipse, Properties->Builders->New)