简体   繁体   中英

Error connecting to derby in embedded mode with gradle dependencies : NoClassDefFoundError SystemPermission

I'm migrating a project into Gradle, and running into a problem with derby. All was well until I added derby dependencies to my build.gradle - I'm getting this exception on connect:

Connecting to SscceDb at jdbc:derby:D:\Sscce\DB//SscceDb;create=true with props [{password=HelloStackOverflow, user=SscceDb, derby.system.home=D:\Sscce\DB}]
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/derby/shared/common/security/SystemPermission
   at org.apache.derby.iapi.jdbc.AutoloadedDriver.connect(AutoloadedDriver.java:134)
   at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
   at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189)
   at org.ferriludium.curator.Sscce.scce(Sscce.java:45)
   at org.ferriludium.curator.Sscce.main(Sscce.java:54)
Caused by: java.lang.ClassNotFoundException: org.apache.derby.shared.common.security.SystemPermission
   at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
   at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
   at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
   ... 5 more

I've looked at several other similar questions, but none of them seem to match my issue.
This is how I'm specifying my dependency in build.gradle, which is what I found by searching for "gradle dependency derby"

implementation 'com.google.guava:guava:28.0-jre', 'org.jsoup:jsoup:1.13.1', 'commons-io:commons-io:2.6', 'org.apache.commons:commons-lang3:3.0', 'org.apache.derby:derby:10.15.1.3', 'org.apache.derby:derbytools:10.3.1.4'

This is my simple example which duplicates the problem:

private String protocol = "jdbc:derby:";
void sscce(String dbName, String storageLoc, String password) {
    Properties props = new Properties(); // connection properties
    props.put("user", dbName);
    props.put("password", password);
    props.put("derby.system.home", storageLoc);
    String dbUrl=protocol + storageLoc + "//" + dbName + ";create=true" ;
    System.out.println("Connecting to " + dbName + " at " + dbUrl+ " with props [" + props + "]"); System.out.flush();
    try {
        Connection conn = DriverManager.getConnection(dbUrl, props);     <=== This is line 45
    } catch (SQLException e) {
        System.err.println("Boom: [" + e.getLocalizedMessage() + "]");
        e.printStackTrace(System.err);
    }
}

For a bit of context, this works if I have my derby dependency in the classpath in a non-gradle way:

Connecting to SscceDb at jdbc:derby:D:\Sscce\DB//SscceDb;create=true with props [{password=SsccePwd, user=SscceDb, derby.system.home=D:\Sscce\DB}]
Connected to SscceDb at jdbc:derby:D:\Sscce\DB//SscceDb;create=true
Adapter connected to org.apache.derby.impl.jdbc.EmbedConnection@1716932897 (XID = 168), (SESSIONID = 3), (DATABASE = D:\Sscce\DB//SscceDb), (DRDAID = null) 

Resolved: I upgraded the versions of derby and derbytools, and now it works using gradle dependencies

I've resolved my issue, and can now run using derby dependencies from gradle. I still don't know the answer to the underlying question of why it worked with eclipse buildpath dependencies but failed with gradle dependencies.

I'm going to be accepting this answer, but if anyone posts with an explanation of why the behavior was originally different between gradle and eclipse, I'll accept that with my thanks.

Here's my new gradle implementation line.

implementation 'com.google.guava:guava:28.0-jre', 'org.jsoup:jsoup:1.13.1', 'commons-io:commons-io:2.6', 'org.apache.commons:commons-lang3:3.0' , 'org.apache.derby:derby:10.15.2.0', 'org.apache.derby:derbytools:10.15.2.0'

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