Skip to content
Advertisement

gradle executable jar can’t include local jar dependencies

SOLVED

WaitingDatabase.connect(WaitingDatabase.java:17)

// added this line
Class.forName("oracle.jdbc.driver.OracleDriver");

connection = DriverManager.getConnection(
        DatabaseProperties.properties.getProperty("waiting_url_oracle"),
        DatabaseProperties.properties.getProperty("waiting_user_oracle"),
        DatabaseProperties.properties.getProperty("waiting_password_oracle")
);

(Sorry for bad english.)

I have to connect my oracle database, so I downloaded oracle jdbc driver and added in my dependencies. It connected well when I run at intellij, so I built executable jar file with gradle.

However, it couldn’t connect to my oracle database. I think it was built without oracle jdbc driver file, because it is a local jar file.

How can I build with all of my dependencies?

If you need more information about my project to solve this problem, please let me know.

Thank you 😀


build.gradle

jar {
    manifest {
        attributes 'Main-Class': 'App'
    }
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.20'

    // https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client
    compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.6.0'

    // https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'

    // oracle 11g jdbc driver
    compile files('lib/ojdbc6.jar')
}

cmd

{directory}> java -jar ./{jarFileName}.jar

error message

java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@{address}:{port}:{sid}
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at WaitingDatabase.connect(WaitingDatabase.java:17)
        at App.connectDatabase(App.java:22)
        at App.main(App.java:5)

WaitingDatabase.connect(WaitingDatabase.java:17)

connection = DriverManager.getConnection(
        DatabaseProperties.properties.getProperty("waiting_url_oracle"),
        DatabaseProperties.properties.getProperty("waiting_user_oracle"),
        DatabaseProperties.properties.getProperty("waiting_password_oracle")
);

file tree

- {root directory}
  - lib
    - ojdbc6.jar
  - src
    - main
      - java
        - App.java
        - WaitingDatabase.java
  - build.gradle

ADDED

db.properties

sleep-millisecond=1000

waiting_db=oracle

waiting_url_oracle=jdbc:oracle:thin:@{address}:{port}:{sid}
waiting_user_oracle={username}
waiting_password_oracle={password}

waiting_url_mariadb=jdbc:mariadb://{address}:{port}
waiting_user_mariadb={username}
waiting_password_mariadb={password}

Advertisement

Answer

What you did is correct:

compile files('lib/ojdbc6.jar')

This will also work:

dependencies {
    implementation fileTree(dir: 'lib', include: '*.jar')

The advantage of this is that it will include all the jars in the lib directory without you having to do it manually.

ojdbc6.jar is meant for JDK 6, as stated here. If you are using > JDK 6, you might want to consider upgrading your ojdbc6.jar and most importantly you might want to check this.

Cheers

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement