Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project compiles fine in IntelliJ, Tomcat says java.lang.NoClassDefFoundError: my/package/name/blah

My project compiles fine in IntelliJ, it is a simple spring-mvc written in scala.

I get this error when I run it using tomcat:

java.lang.NoClassDefFoundError: org/example/houses/SomeClassNameHere

The above isn't the exact name of my library.

My controller looks like:

package com.example.scalacms.web.controllers

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.{ResponseBody, RequestMapping}
import org.example.house

@Controller
class HomeController {
  var houses: Houses = _


  @RequestMapping(Array("/"))
  @ResponseBody
  def index: String = {
    "hello, world!"
  }

}

I'm confused because it compiles fine in IntelliJ, it picks up all my classes in intellisense etc.

Could it be that tomcat doesn't have the library in my classpath? I am using an exploded artifact.

I can't see the classpath anywhere in the output windows so I cannot confirm.

Update

So I looked in the /out/artifacts/myapp_web_war_exploded/WEB-INF/lib and I don't see the jar file there. Why is the jar not being included in the deployment? What should I do to have it included in intellij?

like image 841
Blankman Avatar asked Mar 09 '14 16:03

Blankman


3 Answers

If it's a maven WAR project, check if the jar is marked as excluded or in scope provided in the pom, as any of those would remove the jar from the WAR.

If it's not the case, go to the command line to the directory where the pom WAR is, and do a mvn dependency:tree and confirm that your jar is indeed there.

If the jar is there, then it means it can only be an IDE related problem. Try the following steps:

  • scrap your current server
  • download and unzip a clean server from the official site
  • configure the new server in the IDE
  • make sure to import the module as a maven module, so that IDEA builds both the compilation and the deployment classpaths fully based on the poms
  • If it's already a Maven project, click Maven Projects (hidden pane on the top left) and click Reimport All Maven Projects
  • do a Build -> Rebuild Project
  • Click F4 / Libraries and confirm the library is there, then run the server
like image 181
Angular University Avatar answered Nov 07 '22 11:11

Angular University


i dont know in intellij but in eclipse when you deploy and work with maven it sometimes occurs that you not include your maven dependencies in your deployment assembly

look this :Maven dependencies not visible in WEB-INF/lib

like image 39
Makoton Avatar answered Nov 07 '22 13:11

Makoton


I had the same problem and fixed it now after debugging ...

IntelliJ usually auto-creates a folder /lib under your /src folder inside your project, and then it would tell your project that all LIBs are in there, on that basis; we usually assume that it's the place to put all your LIBs in there as well.

Although with that setup your project would compile fine since IntelliJ can link up to your JARs However, with that sort of setup Tomcat will fail to execute, since Tomcat expects to find the classes under /WEB-INF/lib,...

Therefore, the solution is to:

1) Drag your LIB folder (sorry I mean "/lib") from /src/lib to be under /web/WEB-INF directory

2) You would get a warning about moving classes / JARs, say YES.

(You need to tell your project to re-map your existing pre-defined LIBs to the new folder):

3) From the main menu, select FILE -> Project Structure

4) Select Libraries from the left menu

5) If you don't see any existing libs, then you're done, click OK

6) If you do see libs in there, then:

7) Click on each LIB from the middle-list, and then remove the ones that can't be located,

8) Re-add them again from the new location

9) Repeat (7) to all other LIBs.

10) OK,

RE-compile, your project should deploy on Tomcat now and work fine.

Regards Heider

like image 23
Heider Sati Avatar answered Nov 07 '22 13:11

Heider Sati