Search code examples
javagradlejardependenciesbuild.gradle

Building a Jar using gradle - Jar hasn't sql dependency


I am trying to build a very simple Program. I only have one dependency, the msql-jdbc.

Every build succeds. But if i try to run the jar this exception ist thrown:

Error: Could not find or load main class metaDataDB-0.5-msSQL.jar Caused by: java.lang.ClassNotFoundException: metaDataDB-0.5-msSQL.jar

If i look into the jar, for example 7zip, I see all my compiled Classes.

The content of MANIFEST.MF is as follows:

Manifest-Version: 1.0
Main-Class: metaDataDB.Main

Maybe this helps: Project Structure in Intellij

Here is my full build:

plugins {
    id 'java'
}

jar{
    manifest {
        attributes 'Main-Class': 'metaDataDB.Main'
    }
}

repositories {
    mavenCentral()
}

group 'com.dvb'
//version '1.0-SNAPSHOT'
version '0.5-msSQL'
sourceCompatibility = 17


dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
    // https://mvnrepository.com/artifact/org.postgresql/postgresql
    //implementation group: 'org.postgresql', name: 'postgresql', version: '42.3.1'
    // / https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
    // https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
    implementation group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '9.5.0.jre17-preview'
}

test {
    useJUnitPlatform()
}

Did I setup something wrong? Is this an Intellij error? I am really confused. I think it should work...

EDIT: I did the mistake that i did a wrong java command. The way i did it before:

java metaDataDB-0.5-msSQL.jar -login:login.txt

The right way to do it:

java -jar metaDataDB-0.5-msSQL.jar -login:login.txt

But know I have another Problem. My jdbc driver isn't included in the Jarfile Jarfile Contents


Solution

  • The first problem was, that I ecexuted the Jar file in a wrong way.

    I did

    java metaDataDB-0.5-msSQL.jar -login:login.txt

    The right way to do it:

    java -jar metaDataDB-0.5-msSQL.jar -login:login.txt

    After that, I realized that I didn't include the dependency needed into my Jar. I've learned what a Fat Jar File is.

    So I needed to add this to my gradle jar task:

     from{
            configurations.runtimeClasspath.collect {it.isDirectory() ? it: zipTree(it)}
        }
    

    For referencere my whole build.gradle file:

    plugins {
        id 'java'
    }
    
    jar{
        manifest {
            attributes ('Main-Class': 'metaDataDB.Main')
        }
        from{
            configurations.runtimeClasspath.collect {it.isDirectory() ? it: zipTree(it)}
        }
    
    }
    
    repositories {
        mavenCentral()
    }
    
    group 'com.dvb'
    //version '1.0-SNAPSHOT'
    version '0.5-msSQL'
    sourceCompatibility = 17
    
    
    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
        // https://mvnrepository.com/artifact/org.postgresql/postgresql
       // implementation group: 'org.postgresql', name: 'postgresql', version: '42.3.1'
        implementation 'com.microsoft.sqlserver:mssql-jdbc:10.1.0.jre17-preview'
        // / https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
        // https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
        //implementation 'com.microsoft.sqlserver:mssql-jdbc:9.2.1.jre15'
    
    }
    
    test {
        useJUnitPlatform()
    }
    

    That worked for the postgressql Driver.

    For the Microsoft SQL Drivers a new Problem came up.

    java -jar metaDataDB-0.5-msSQL.jar -login:ms.txt
    Error: A JNI error has occurred, please check your installation and try again
    Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
            at java.base/sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:340)
            at java.base/sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:282)
            at java.base/java.util.jar.JarVerifier.processEntry(JarVerifier.java:277)
            at java.base/java.util.jar.JarVerifier.update(JarVerifier.java:234)
            at java.base/java.util.jar.JarFile.initializeVerifier(JarFile.java:762)
            at java.base/java.util.jar.JarFile.ensureInitialization(JarFile.java:1033)
            at java.base/java.util.jar.JavaUtilJarAccessImpl.ensureInitialization(JavaUtilJarAccessImpl.java:72)
            at java.base/jdk.internal.loader.URLClassPath$JarLoader$2.getManifest(URLClassPath.java:883)
            at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:848)
            at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
            at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
            at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
            at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
            at java.base/java.lang.Class.forName0(Native Method)
            at java.base/java.lang.Class.forName(Class.java:467)
            at java.base/sun.launcher.LauncherHelper.loadMainClass(LauncherHelper.java:780)
            at java.base/sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:675)
    

    here a link to the exception:

    https://learn.microsoft.com/de-de/dotnet/api/system.security.securityexception?view=net-6.0

    I tried it to run in adminstrator mode, but that failed too...

    But I guess this is a new question, and hasn't to do anything with gradle.

    I guess the answer to "Building a Jar using gradle - Jar hasn't sql dependency" is answered.

    I will create a new Question for the microsoft sql driver and link it here.

    (I hope I did everything right)