在Intellij gradle项目中运行main类时出现ClassNotFoundException

时间:2016-05-27 13:26:37

标签: java intellij-idea gradle

当我尝试使用IDEA Intellij选项中的运行配置在我的gradle项目中运行main方法时,我得到如下异常:

15:12:03: Executing external task 'run'...
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/GsonBuilder
    at pl.edu.agh.flowshop.utils.ConfigReader.getMachinesConfig(ConfigReader.java:46)
    at pl.edu.agh.flowshop.utils.ConfigReader.createModel(ConfigReader.java:30)
    at pl.edu.agh.flowshop.Experiment.main(Experiment.java:25)
Caused by: java.lang.ClassNotFoundException: com.google.gson.GsonBuilder
    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)
    ... 3 more

这让我感到困扰,因为如果我只是从相同的配置创建可运行的jar一切正常。我已经尝试刷新gradle项目以及'Invalidate Caches / Restart'选项,但它们都没有帮助。

我的build.gradle文件:

group 'pl.edu.agh'
version '1.0-SNAPSHOT'

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

sourceCompatibility = 1.7
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = ['src/main/java']
mainClassName = 'pl.edu.agh.flowshop.Experiment'

repositories {
    mavenCentral()
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile group: 'nz.ac.waikato.cms.weka', name: 'weka-stable', version: '3.6.6'
    compile group: 'commons-io', name: 'commons-io', version: '2.4'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
    compile group: 'org.apache.commons', name: 'commons-math3', version: '3.6'
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
    compile group: 'com.google.guava', name: 'guava', version: '19.0'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.6.2'
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.5'
    compile name:'piqle'
}

jar {
    manifest {
        attributes(
                'Implementation-Title': project.name,
                'Implementation-Version': version,
                'Built-By': System.getProperty('user.name'),
                'Built-Date': new Date(),
                'Built-JDK': System.getProperty('java.version'),
                "Main-Class": mainClassName
        )
    }
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}

0 个答案:

没有答案