Stacky
Stacky

Reputation: 905

JDBC connection works in IDE (eclipse), but not in .jar file

I'm really looking at a mystery here. I created a Java program in Eclipse and established a JDBC connection. The code is the following:

import java.sql.*;

public class Login {

    public static void main(String[] args) {
        try {
            // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection connection = DriverManager
                    .getConnection("jdbc:sqlserver://localhost;databaseName=testdb; integratedSecurity=true;");
                                   //adding port 1433 doesn't make a difference 

            System.out.println("Connection successful");
            Statement st = connection.createStatement();
            ResultSet rs = st.executeQuery("select * from testtable");

            while (rs.next()) {
                line = rs.getString(2);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

When I'm running it within eclipse, everything's fine. The database and tables are good, library and authentication in the Java Built Path are set and I get the result I want, namely "Hello World", which is a string in the selected table.

But creating an executable JAR file and running it throws the exception "This driver is not configured for integrated authentication". It does not even connect, so even if there was an error in the database it wouldn't matter at this point.

Even more confusing is the fact, that some weeks earlier, I also created an executable JAR file and it worked!

OS: Windows Vista Home Premium x86

JDBC driver location: "C:\sqljdbc_4.0\enu\sqljdbc4.jar"

Native library location: "C:\sqljdbc_4.0\enu\auth\x86"

Java version: Java 7 Update 17 and JDK 7 Update 17

Previousely, I used Update 21, but I changed back to check if the driver could be the reason.

So, any suggestions? I'd be very grateful!

Upvotes: 0

Views: 8831

Answers (2)

vanomart
vanomart

Reputation: 1839

Actual problem is that if you run your project from eclipse, it will automatically add run arguments that are defined in run/run config//arguments, so you run your program with properly set path to sqljdbc_auth.dll. But if you export your application to .jar file and just run it, you will get these errors cause there is no correct classpath to auth file.

So, if you run your app from command line with proper arguments, it should be working.

Upvotes: 0

Chris
Chris

Reputation: 5654

  • Check the runtime classpath of eclipse, then try to create a system level "CLASSPATH" variable and add the same path. Once the variable is set, execute it in new command prompt or reload the environment variables

  • Make sure your executable jar has the dependency in MANIFEST.MF for all the necessary jars (along with MainClass)

  • Finally, It is always a practice in Java to make the first alphabet in Capital ("Login" instead of "login")

Upvotes: 2

Related Questions