简体   繁体   中英

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver after creating a jar

I've established a database connection in my program but after creating a jar program it is throwing an

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

It looks like program don't see jdbc driver which I have installed and added to artifact:

在此处输入图片说明

I've tried changing a versions changing link looking for class name in JDBC but I've found same one that I've used before which worked in intelij but not in jar

There is a code that I'm using for connection

package howareyoufeeling.database;

import java.sql.*;

public class DatabaseOperations {
    static Statement statement;

    public static void connectWithAzureDatabase() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String connectionUrl =
                "jdbc:sqlserver://howareyoufeeling.database.windows.net:1433;" +
                        "database=HowAreYouFeeling;" +
                        "user=wikimmax@howareyoufeeling;password=********;" +
                        "encrypt=true;trustServerCertificate=false;" +
                        "hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
        try {
            Connection connection = DriverManager.getConnection(connectionUrl);
            System.out.println("Successfull DB connection " + connection.getSchema());
            statement = connection.createStatement();

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

        }
    }
    public static void sendDataQuery(String query){
        try {
            statement.execute(query);
        } catch (SQLException e) {
            e.printStackTrace();

        }
    }
}

It looks like dependency is not in jar file but it is i checked it by decompiling and file size matches that includes a driver

Please help

It's a packaging issue if you get that exception at runtime.

Your executable JAR needs to have all the dependencies built inside it. There's a Maven plugin that can do it for you.

I'd recommend moving your database configuration outside of code so you can modify it more easily.

Problem has been solved. The main wrong thing there was that I've tried to build a jar as application not as as Javafx app and database jar file was inside my build jar

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