0

I'm setting up a SQL connector for my bukkit plugin, but whenever I compile into a jar file and try running from the server I get

java.sql.SQLEXCEPTION: No suitable driver found.

I have tried adding in the line

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Whenever I put that I get

SQLEXCEPTION: com.microsoft.sqlserver.jdbc.SQLServerDriver

This problem only occurs when I put it into a jar file and not when I test it in my IDE.

I currently use Intellij.

This is my current Jar setup: https://gyazo.com/94341b7bb47121a0416deaee6279dd30

 public ConnectionUtils(String url, String us, String pa) throws SQLException
    {
        SQLServerDriver dr = new SQLServerDriver();
        user = us;
        pass = pa;
        con = DriverManager.getConnection(url, us, pa);
        isConnected = true;
        this.url = url;
    }
Dale K
  • 25,246
  • 15
  • 42
  • 71

1 Answers1

2

Solution using Project Structure

Outside of using maven, you can look at your Project Structure in IntelliJ and examine your dependencies. Make sure the "export" box next that jar is checked.


Maven Solution

I recommend using maven to handle your dependencies, as you can define the scope of the dependency, as explained here.

For the JDBC dependency, you could use the following dependency declaration in your pom:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.17</version>
</dependency>

This would inherit the default scope which, as stated in the article, is compile.

3.1. Compile

This is the default scope when no other scope is provided.

Dependencies with this scope are available on the classpath of the project in all build tasks and they're propagated to the dependent projects.


Manifest

It seems some people online are talking about exactly this issue, and the solution is one that @Bohemian mentioned for ensuring that the required class is packaged with the jar. However, that solution only works if you are executing the jar from the command line, which is not the case with spigot plugins. I suggest creating a MANIFEST.txt and including the driver class-path in there, as suggested by Terence Gronowski on CodeRanch

Creating a Manifest.txt file with the following contents in the program folder:

Manifest-Version: 1.0 Class-Path: sqljdbc4.jar Main-Class: ParkplatzVerwaltung (Newline)

Don't forget to end with a newline. The important thing is "Class-Path: sqljdbc4.jar", showing where the driver is.

Source: https://coderanch.com/t/529484/databases/Jdbc-driver-putting-application-jar

Zack R
  • 207
  • 1
  • 11
  • Added in my pom.xml: mysql mysql-connector-java 8.0.17 com.microsoft.sqlserver mssql-jdbc 7.4.1.jre11 Also attempted using the checkbox on the dependencies and still the same results – Yukito The Fox Sep 23 '19 at 02:52
  • Take a look at editing the jars manifest file to include your required Class. https://coderanch.com/t/529484/databases/Jdbc-driver-putting-application-jar – Zack R Sep 23 '19 at 03:11