简体   繁体   中英

NoClassDefFoundError while handling asm dependency with gradle

When I tried to use gradle to build a program using asm package, I got the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
    at analysis.CCMetric.main(CCMetric.java:5)
Caused by: java.lang.ClassNotFoundException:org.objectweb.asm.ClassVisitor
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

My gradle script is as follow:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'

mainClassName = 'analysis.CCMetric'

repositories {
    maven { url "http://repo.maven.apache.org/maven2" }    
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile 'org.ow2.asm:asm-all:5.0.2'
    compile 'commons-io:commons-io:2.4'
    compile 'org.jgrapht:jgrapht-core:0.9.1'
    compile 'asm:asm:3.3.1'
}

jar {
    manifest {
        attributes 'Main-Class': 'analysis.CCMetric'
    }
    baseName = 'CCMetric'
    version = '1.0.0'
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
}

I used gradle build to build the project and I run the jar file with java -jar build/libs/CCMetric.jar and then I got the exception as above.

I have found the solution from Gradle Forum. Now I'm pasting the answer from Muschko, B.

You will also have to provide the external JARs you are depending on when executing the Java command. Gradle does not hook into the Java command magically and provide the external libraries. A good alternatively you can look into is the Application plugin which does something you are looking for out-of-the-box with the run task.

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