Gaurav Pant
Gaurav Pant

Reputation: 343

How to solve cyclic dependency between different modules in a project in eclipse?

The Problem log in eclipse shows "A cycle was detected in the build path of the project ...." Any idea what to do to get rid of these cyclic dependencies? I don't know which projects are dependent on each other.

Upvotes: 19

Views: 67029

Answers (7)

HareshKannan
HareshKannan

Reputation: 103

There are three ways to remove cyclic dependency between projects in eclipse,

1.Go to project-> java compiler-> building -> Enable project specific settings.
Select build path problems and give warning as option for circular dependency.

2.Go to project->java build path. In projects tab, select the project and remove.

3.Go to your META-INF folder, open MANIFEST.MF. In your MANIFEST.MF view tab, you can see the cyclic dependent project in "Import package:" column.
Remove the project from the column.

The first option does not really resolve the error. The second and the third option are the right way to resolve this dependency.

Upvotes: 4

Jerry Miller
Jerry Miller

Reputation: 981

I had run into this issue a long time ago, and even though I've forgotten how I resolved that one, when it came to communicating from a class in the required project to one in the requiring project, I had the sendee install a java.lang.reflect.Method object in the sender, where it could be invoked without declaring the sendee class within the sender's code.

I'm sure there are preferred ways of doing this, such as having the sender and sendee share an interface, but in terms of coding economy, this seems to be the simplest.

As other answers seemed to suggest, this does not appear to be a true circularity, but rather an artifact of the Eclipse design.

Upvotes: -1

Balint Bako
Balint Bako

Reputation: 2560

You might have to refactor your projects or the code itself. It might be an issue with the packaging with you application.

Upvotes: 0

TecHunter
TecHunter

Reputation: 6141

In project setting you can see the dependencies, what you could do is removing all dependencies and add one dep after another. A cycle means you shouldn't have done it that way. Your solutions after identifying the classes, let says your big Project A is requiring Class CB1 from secondary Project B and CB1 is requiring class CA1 from project A :

  • Move a class CB1 in the project A to remove the dependency --> works if it doesn't create more dependencies.
  • extract interfaces and use non dependant interfaces (you might need to create super class or more interfaces depending of your structure).
  • implement differently, why do you specifically need class from other project? try using common parent classes if any

Upvotes: 3

Marko Topolnik
Marko Topolnik

Reputation: 200216

To find out the dependencies between plain Java projects, go see Project Properties | Java Build Path | Projects. If you have J2EE projects, there is an additional setting in Project Properties | Project References. Check those for all your projects, build the graph on a piece of paper (or screen) and see what you can do to break the cycle.

Usually, a healthy dependency graph has the form of a star, with a core project containing common resources, and each leaf project implementing some specific user functionality. In detail, the core may in fact consist of several interdependent projects; same for larger leafs.

A typical way out of a dependency cycle is the merging of several closely coupled projects.

Upvotes: 0

Rajesh J Advani
Rajesh J Advani

Reputation: 5710

A cyclic dependency in eclipse indicates that there is a cycle in the buildpaths between projects in Eclipse.

So if you have 5 projects, say A, B, C, D and E, then a cyclic dependency could be that:

  1. A requires B in its build-path
  2. B requires D in its build-path
  3. D requires A in its build-path

Hence A->B->D->A is a cycle.

Because of this cycle, Eclipse does not know which project to compile first.

You need to refactor your code to remove this cyclic dependency. Or if the actual code doesn't have such dependencies, remove the build-path entries which are unnecessary.

Upvotes: 2

JREN
JREN

Reputation: 3630

You can adjust the circular dependencies severity in eclipse:

Preferences > Java > Compiler > Building > Build path problems > Circular dependencies

Upvotes: 20

Related Questions