Tomcat throws ClassNotFound exceptions for classes in other open eclipse projects
Asked Answered
S

6

23

I have an eclipse project structure that looks something like this:

eclipse
    project a
    project b

All of these projects are Maven projects. Project B is an Eclipse WTP project and contains Project A as a dependency. I'm using the m2eclipse plugin.

When I deploy Project B to Tomcat (all from within Eclipse) it always throws errors saying that it can't find classes from Project A. The only way I can resolve this is to close Project A in Eclipse and do a mvn install command, so at this point Project A is treated like any other dependency.

I would like to not have to keep reopening and then closing Project A every time I make changes to it (which is pretty frequently at this stage).

I have tried adding Project A to Project B's Build Path, however that doesn't seem to do the trick. Again, it only works if I close the project in eclipse so it's treated like a normal maven dependency.

FYI I'm using the Helios version of Eclipse. Project B is a Spring project, so naturally Tomcat falls over when it imports all the beans etc.

Does anyone have any suggestions? Thanks a lot.

Sikkim answered 9/4, 2011 at 8:14 Comment(0)
P
76

What you need to do is this:

  1. In Eclipse go to Window > Show View > Servers > and open tab called Servers
  2. In Servers, double click the server you want
  3. This action should open a window entitled Overview, with your selected Server in the TAB
  4. In this window select: Open launch configuration, and click the link.
  5. In the Edit Configuration window, select the tab called Classpath
  6. In the ClassPath textbox, select the line: User Entries
  7. Now push the button on the right entitled: Add Projects
  8. In the resulting Project Selection window select the external project you want the server to find via click.
  9. Close these windows by clicking Apply then OK as appropriate as you work up the stack
  10. Restart your server in the Overview window

You should be ready to go. It's actually only a 2 minute exercise. Good luck

Pilch answered 11/9, 2012 at 3:38 Comment(2)
This works. I feel a lot of programmer guilt doing it, but it works. Perhaps someday m2e will handle the deployment of project dependencies automatically.Assorted
Finally this solution worked for me with similar issue of maven dependencies, thanks a ton!Farseeing
C
35

If you go to the project properties, pick the item "Deployment Assembly", click "Add..." and choose "Project", you can add the dependent project and the issue should be solved.

Converge answered 11/3, 2012 at 20:44 Comment(4)
Jalkin's solution with the run configuration worked too, but this seems a lot cleaner, thank you.Fascination
Future reader, don't go nuts if this fix doesn't work for you. It doesn't work for me, either. :-(Assorted
3+ years later and this is still an issue in Eclipse Luna. Odd indeed as I've added modules to my project in the past with no issues, but for some this last module add didn't work. I had to use Add => Folder, but it worked the same. Thanks!Anibalanica
I would say this is the solution I have been looking for. Web modules and EJB modules can be deployed independently (without the EAR).Gahan
S
5

The reason that tomcat cannot find your classes is that the project dependency in eclipse is not seen by tomcat. To resolve the issue you can add the bin folder of project a to the tomcat classpath of the app running from project b.

Spikenard answered 9/4, 2011 at 8:20 Comment(0)
A
5

Right click on the web project which is added to container server and select Maven and then select Disable Workspace Resolution. Then your dependency project will start appearing as a jar like any other dependencies.

Right Click on the WebProject --> Maven --> Disable Workspace Resolution.

Asseverate answered 10/5, 2013 at 8:39 Comment(1)
This also works (just tested it). It has the downside of requiring you to run mvn install on "project a" each time something changes there, though, since "project b" now depends on the artifact in your repo.Assorted
M
2

Have you installed the m2eclipse extension that know about WTP? Without it m2e will not work correctly for WTP projects. See this:

https://docs.sonatype.org/display/M2ECLIPSE/WTP+mini+howto

Mansell answered 9/4, 2011 at 16:26 Comment(0)
R
0

Simple way :

Run -> Run Configurations... -> Classpath

Ransom answered 20/3, 2017 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.