简体   繁体   中英

Could not find or load main class with gradle generated jar file

I stuck at this stage for hours and can not figure out what I am missing. Maybe someone sees the obvious mistake...

In the first step I build a jar file with gradle:

jar {
    manifest {
        attributes(
                'Main-Class': 'ch.hsr.ebos.offliss.server.Main',
                "Class-Path": configurations.compile.collect { it.getName() }.join(' ')
        )
    }
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

I let gradle build the jar file and when you try to execute it, the following error message appears:

java -jar build/libs/server.jar 
Error: Could not find or load main class ch.hsr.ebos.offliss.server.Main

When you unzip the content of the jar file there is the META-INF/MANIFEST.MF :

Manifest-Version: 1.0
Class-Path: 
Main-Class: ch.hsr.ebos.offliss.server.Main

and the Main.class file under the directory ch/hsr/ebos/offliss/server .

Not sure whats missing and have no idea to go any further. The error is pretty common on stack overflow, but the other questions and answers didn't help me so far.

The whole source code is open source and available here:

Fixed the issue finally by changing the gradle build configuration to:


jar {
    manifest {
        attributes(
                'Main-Class': 'ch.hsr.ebos.offliss.server.Main'
        )
    }
}


task fatJar(type: Jar) {
    manifest.from jar.manifest
    classifier = 'all'
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    } {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }
    with jar
}

You can simply combine both jar and fatJar to:

jar {
    manifest {
        attributes(
                'Main-Class': "org.cip4.tools.alces.swingui.Alces"
        )
    }
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    } {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }
}

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