-1

I am trying to run a spring project in intellij. When I import the code and add the necessary jars, it there is no error during compile time. However, when I run the project, I get the following error

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

The KeyValueAccess class is present in the cassandra-access-1.12.0.jar as highlighted in the image.

I have tried to add jars as modules and also as libraries. What is the correct way to avoid this runtime error?

enter image description here

PS: This is the case for only manually added external libraries. The libraries downloaded with maven work just fine.

Edit

The following code generates the exception

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

Exception generated is as below:

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

I also checked that the jar does it fact have the required class files.

Edit2

I manually ran maven to add the jar files, so I can now find them in ~/.m2/repository. When I call a class in the jar, it seems to find it but when that class calls another class in the same jar, it gives java.lang.NoClassDefFoundError

kosta
  • 4,302
  • 10
  • 50
  • 104
  • If it's a war application then delete all teh content in output folder and then rebuild and run again. – Moshiour Nov 07 '16 at 04:33
  • check my updated answer – Moshiour Nov 07 '16 at 09:20
  • *PS: This is the case for only manually added external libraries. The libraries downloaded with maven work just fine.* This is a problem: if you're working with Maven, all of your dependencies should be in the POM. Not added manually through IntelliJ. The NoClassDefFoundErrors are because the JARs are not present at runtime. – Tunaki Nov 07 '16 at 22:42
  • @Tunaki He wrote this "Yes, I did add those cassandra libraries as they are company specific.". So libraries are necessary. – mchern1kov Nov 08 '16 at 06:59
  • Please, add this line to question – mchern1kov Nov 08 '16 at 07:00
  • @MaksymChernikov The libraries are necessary yes... but they **must** be added in the POM. If they're not on Maven repo, then refer to http://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-in-maven-project – Tunaki Nov 08 '16 at 07:47
  • @Tunaki check my answer) I've already said it. But it does not work either. – mchern1kov Nov 08 '16 at 08:19
  • There is too little information to answer anything here. I still didn't see any POM. Suffice it to say that if the exceptions are thrown, the libraries are not correctly declared. – Tunaki Nov 08 '16 at 08:21

1 Answers1

0

As I see in your screen, you added cassandra manually, not using maven? So this may be the problem. Add dependencies to pom.xml. Watch them here.

Also check if your dependencies added to special module, not just in libraries. Go to file->project structure->modules. Choose your module in which you use cassandra and click on "dependencies" tab. Press "+" and "library" from dropdown menu and then add your libraries to project.

mchern1kov
  • 450
  • 3
  • 10
  • Yes, I did add those cassandra libraries as they are company specific. I also added the `casssandra-all` library using maven as you suggested (as per my cassandra version). I have also added the libraries manually in the same way as you suggested. However, it is still not working. – kosta Nov 07 '16 at 08:40
  • @user3288346 if they are company specific, try to add them as a local dependency. Watch it [here](http://stackoverflow.com/a/22300875/5783044). Also if it's war package .jar file would not be added to .war file so you need compile plugin – mchern1kov Nov 07 '16 at 09:28
  • @user3288346 or try to add it to your local repo. Watch [here](http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html). But this may cause problem using version control. Only you have this file in your local repo until others also put it manually – mchern1kov Nov 07 '16 at 09:33
  • Just running it locally is fine. I tried the same. It still gives the same error. I also checked that in the `~/.m2/repository` folder the appropriate directory is created. It seems to be ok, however still having a tough time ti get rid of the error. – kosta Nov 07 '16 at 09:53
  • @user3288346 by the way, is your project .war or .jar? Do you have your library in target? – mchern1kov Nov 07 '16 at 10:01
  • it's a `war` project. – kosta Nov 08 '16 at 01:09
  • Can you also check `edit2` in the original question? – kosta Nov 08 '16 at 01:12
  • @user3288346 hm. Rather strange. Are you sure you have all necessary in classpath? You need to ensure all your libraries are put into .war in target. They are in m2 but it does not mean, they are added to your .war so they can throw this exception in runtime – mchern1kov Nov 08 '16 at 07:02
  • @user3288346 If they are not added, i think you need maven plugin for jars – mchern1kov Nov 08 '16 at 07:04