2016-01-06 40 views
1

當我試圖使用gradle這個使用ASM包建立一個程序,我得到了以下錯誤:NoClassDefFoundError的同時處理ASM依賴與gradle這個

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 

我gradle這個腳本是如下:

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' 
} 

我使用gradle build來構建項目,並使用java -jar build/libs/CCMetric.jar運行jar文件,然後得到如上所述的異常。

回答

0

我從Gradle Forum找到了解決方案。現在我粘貼來自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.