7

I have two Java projects called A and B. Both of them are web apps deployed as war files. I created them in Eclipse workspace separately. Project B uses a class in project A named MusicMapper. I added the project A to project B's build path in Eclipse as suggested in this post. So now project B can compile without any errors and the class MusicMapper can be seen in project B by importing it in project B:

import com.projectA.MusicMapper;

Everything seems to be fine before I launched the web app of project B. However, when I launched the project B and called the code that references class MusicMapper in project A, I got the following runtime error:

java.lang.ClassNotFoundException: com.projectA.MusicMapper

So this error seems to be caused by unfound class com.projectA.MusicMapper which is imported from project A. Since I already added project A to project B build path and project B compiles fine without any errors, why does it report this error at runtime?

Another approach I took was: I've also tried using the following Maven import in project B's pom.xml:

    <dependency>
        <groupId>com.projectA</groupId>
        <artifactId>projectA</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>system</scope>
        <systemPath>/path/to/projectA.jar</systemPath>
    </dependency>

where projectA.jar is the jar file I exported from project A. But it still gave me the same ClassNotFoundException. How can I make the class in project A usable by project B? It seems that neither of the two approaches I've tried works.

Community
  • 1
  • 1
tonga
  • 11,749
  • 25
  • 75
  • 96

1 Answers1

7

First of all if you have two projects which are both deployed as wars it's not a good idea to include one project into another as a dependency. Because you will be puling in lot of other stuff that you don't need in project B.

A better approach will be to create a Third java project lets say "Common" this should be just a java project and NOT a Dynamic Web Project and should compile as a jar. Move all the stuff that is shared between Project A and Project B into Common project.

Now back your problem Just adding some thing in project build path in eclipse does not mean you have added the dependency to your project outside of eclipse as well. Eclipse don't complain because it can resolve and find the project because you added in eclipse build path. You will have to compile common dependency project using some build tool like ant, maven or gradle and package the jar in your war file when war is built.

If you don't want to use a build tool a simple route would be just export the third project from eclipse as jar. Copy the common jar file in WEB-INF/lib folder of your Project A and Project B before you export the war file from your eclipse.

I hope it helps :)

vkg
  • 1,839
  • 14
  • 15
  • Thanks. So I need to move the `MusicMapper` class out of project A and put it in the "Common" project which is packed in to a jar. But what if the class `MusicMapper` has some dependency on some classes of project A? Also, is there any way that I can just include the jar exported from project A in project B's Maven pom.xml? I tried it as shown above but didn't succeed. – tonga May 16 '14 at 22:14
  • Lots of questions :) Yes you should move the MusicMapper to common project. But now if you find that MusicMapper class has lots of dependencies which results in pulling pretty much all of project A that reflects a bad design in code. You might have to do some refactoring to cleanup in that case. Now back to maven. It's really hard to explain all about that here. But the basic concept it during build maven will first compile the common project and publish to local mvaen repo. And if you have defined the dependencies correctly it will include that jar when packaging the war of your web project. – vkg May 16 '14 at 22:19
  • Yes, I think if `MusicMapper` has a lot of dependencies on other classes of project A, then that means the design is problematic. I will try to set up a "Common" project for common shared classes between project A and B. But on the other hand, will using a master Maven project which includes both projects A and B help? This way, I may not need to move any classes to a common project. But the downside is I have to setup the master Maven project's pom.xml correctly. I've seen people use it that way, too. – tonga May 16 '14 at 22:28
  • Would you consider using gradle? It's much simpler in my opinion to use – vkg May 16 '14 at 22:29
  • Have heard of it but haven't tried it yet. Will try it sometime later when I have time. – tonga May 16 '14 at 22:30
  • I found an easier way to include jar file directly to project B. Since project B is also a war project, I just made a jar based on project A and dropped it in `WEB-INF/lib` of project B. This makes all classes in project A usable by project B. Anyway thanks and I accept your answer as the solution. – tonga May 17 '14 at 02:13