6

So, I have the following setup in Eclipse (Java):

  1. I have a project (lets call this "project 1") which provides an interface (which is package private)
  2. I have another project ("project 2") which contains a package from project 1... Classes in this package extend the package private interfaces in "project 1" and provide a nice public interface.
  3. Now, I have "project 3" which references "project 2". Project 3 then uses the public object provided in project 2.

Upon calling the constructor from project 2 in project 3, I get the following:

"the type {---} cannot be resolved. it is indirectly referenced from required .class files"

If I add a reference to project 1 from project 3, all is well. Is there a way to NOT have to reference project 3 in this way though? It really doesn't make sense to reference it.

I'm using Eclipse (Helios) with the most recent JDK etc...

DigitalZebra
  • 39,494
  • 39
  • 114
  • 146
  • Are we talking about basic java or OSGi (eclipse plugin development or RCP)? With OSGi, each plugin(-project) will be loaded by a separete classloader and this could explain your problems too. – Andreas Dolk Aug 12 '10 at 06:14
  • we are talking about basic java and project dependencies – DigitalZebra Aug 12 '10 at 14:25

2 Answers2

7

This is a "transitive dependency". You need on your classpath all classes that are required by any class you use. So you need to have the classes from project 1 in the classpath somehow - you can package them as a .jar, for example. Or you can go to Build Path > Order and Export of Project 2, and mark Project 1 as exported.

An important thing here is that the project dependencies are a mere development "goodie" - when you execute the program standalone (or deploy it to container), the Eclipse project dependencies are gone. So you must ensure your dependencies are met.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Actually, this is not strictly true with regard to the compile time classpath...it is possible for your dependencies to have dependencies that you do not have to know about. Adding explicit dependencies on every explicit dependency of a jar you use would be an excessive burden. Those dependencies must be available at run time, but not at build time. – Theodore Murdock Nov 10 '15 at 01:46
  • I know this to be true because Eclipse will often log an error message similar to the one that the OP mentions if it is trying to calculate or display the set of supertypes of a class but cannot due to not having all supertypes on the classpath it is using to do the calculation. It does this for many working projects without any runtime issue. – Theodore Murdock Nov 10 '15 at 01:57
2

Not sure if this is what you're looking for. Try this -

  1. In Eclipse, right-click Project2, select Build Path -> Configure Build Path
  2. Go to Order and Export tab. it should have Project1 listed. Check the box in front of it.
  3. Rebuild your projects.

Basically, you're exporting the Project1 dependency from project2. So any project that adds project2 as a dependency will see Project1 too without explicitly having to add it to the classpath. Eclipse will transparently do that.

samitgaur
  • 5,601
  • 2
  • 29
  • 33