Project JAR unable to access Derby Driver

KyleAure :

I am working on a Java project where we want to use an embedded database using JDBC for connection and Derby as the database. This is a maven project which deploys to an executable JAR.

I have included Derby as a dependency on the project. I am using Java 1.8, Maven 3.8.0, and Derby 10.8.3.0.

POM.xml

<!-- Derby for database connection -->
<dependency>
  <groupId>org.apache.derby</groupId>
  <artifactId>derby</artifactId>
  <version>10.8.3.0</version>
</dependency>

App.java

public class App {

public static void main(String[] args) {
    JOptionPane.showMessageDialog(null, "Hello World.");


    SampleDB sdb = new SampleDB();
    try {
        sdb.connectionToDerby();
        sdb.sampleDBUsabe();
    } catch (SQLException sqle) {
        System.out.println(sqle);
    }
}
}

SampleDB.java

public class SampleDB {
private Connection conn;

public void connectionToDerby() throws SQLException {
    String dbURL = "jdbc:derby:sampledb;create=true";
    conn = DriverManager.getConnection(dbURL);
}

public void sampleDBUsabe() throws SQLException {
    Statement stmt = conn.createStatement();

    //Create Table
    stmt.execute("Create Table USERS (id int primary key, name varchar(30))");
...
}

When I execute the generated .JAR file I get the error:

$ java -jar target/puzzle.game-0.0.1-SNAPSHOT.jar
java.sql.SQLException: No suitable driver found for jdbc:derby:sampledb;create=true

I suspect this is due to the Derby driver not being a part of my class path. Is there a way to do this using maven or a settings.xml file so that this happens automatically when a user runs the mvn install goal?

Maxim Kirilov :

See an answer to this question: Building a fat jar using maven . You need to use the maven assembly plugin in order to create a so called fat/uber jar.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=152368&siteId=1