' NoClassDefFoundError的'在Intellij

时间:2016-11-07 04:03:14

标签: java spring intellij-idea

我正在尝试在intellij中运行一个spring项目。 当我导入代码并添加必要的jar时,它在编译期间没有错误。但是,当我运行该项目时,我收到以下错误

Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: com/company/access/KeyValueAccess

KeyValueAccess类出现在图片中突出显示的cassandra-access-1.12.0.jar中。

我尝试将jar作为模块添加,也作为库添加。 避免此runtime错误的正确方法是什么?

enter image description here

PS:仅手动添加外部库就是这种情况。使用maven下载的库工作正常。

修改

以下代码生成异常

@Bean
    public KeyValueAccess keyValueAccess() {
        return new CassandraAccessDatastax(); //This doesn't matter, the return type itself creates the exception
    }

生成的异常如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.worksap.bootcamp.webeditor.config.WebEditorConfig#0' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: com/company/access/KeyValueAccess

我还检查过jar确实有必要的类文件。

EDIT2

我手动运行maven添加jar文件,所以我现在可以在~/.m2/repository中找到它们。当我在jar中调用一个类时,它似乎找到它但是当该类在同一个jar中调用另一个类时,它会给出java.lang.NoClassDefFoundError

1 个答案:

答案 0 :(得分:0)

正如我在屏幕上看到的那样,您手动添加了cassandra,而不是使用maven?所以这可能是问题所在。将依赖项添加到pom.xml。观看他们here

还要检查您的依赖项是否添加到特殊模块,而不仅仅是库中。转到file->项目结构 - >模块。选择使用cassandra的模块,然后单击" dependencies"标签。按" +"和"图书馆"从下拉菜单中,然后将您的库添加到项目。