7

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?

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • Is the code deployed in the exploded artifact? – Giovanni Botta Mar 09 '14 at 17:04
  • Everything works fine if I remove the ``var houses: Houses = _`` code, it runs and outputs 'hello world' just fine. If I add it back, it crashes with that same error. I'm not sure where exactly it deploys too folder wise. – Blankman Mar 09 '14 at 17:47
  • If you are physically deploying the (exploded or not) WAR file you can just unzip it and look at the WEB-INF/classes folder or any jar file in the lib folder to see if your class files are being deployed. It looks like the missing class might be `Houses`. – Giovanni Botta Mar 10 '14 at 01:18
  • Yes so the jar files are not in the lib directory, why would intellij ignore what is in the maven pom.xml file? Is it because it is a scala jar? – Blankman Mar 10 '14 at 02:13
  • In the maven web module, I see my library jar file in the dependancies tab, why isn't it being included? – Blankman Mar 10 '14 at 02:22
  • 1
    Blankman, your question is too abstract for anyone to answer, without guessing. Post the real name of your class, the imports in the controller and the portion of the `pom.xml` file which is declaring the dependency to the jar file containing `org.example.house.*`. – Anthony Accioly Mar 12 '14 at 20:54
  • We dont need real names, but we do need to see the dependency section in your POM and how you are building the WAR file. – cowls Mar 17 '14 at 13:39

3 Answers3

7

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
Angular University
  • 42,341
  • 15
  • 74
  • 81
1

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

Community
  • 1
  • 1
Makoton
  • 443
  • 3
  • 14
1

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

Heider Sati
  • 2,476
  • 26
  • 28
  • Great answer! My issue was that a certain necessary jar wasn't getting copied over to the WEB-INF folder so tomcat wasn't finding it there. – Siddhartha May 07 '20 at 21:06