1

I am trying to develop my application in different Eclipse Java projects where each will contain a certain feature. Then I want to combine them in one complete Java project.

However, I have a problem when linking the sources.

The sub-projects can correctly refer to parent-project classes but some of the source files that are accessed by the parent projects cannot be identified in the sub-projects.

I have a workspace/ParentProject/src/main/resources/file, where in the ParentProject I am accessing with "src\main\resources\" from within Java.

However, at runtime the ChildProject throws an exception that they cannot access the file : 'file:/E:/Eclipse%20workspace/ChildProject/src/main/resources/file'

So, when using a method of the ParentProject from the ChildProject, the classpath is somewhat transfered to the ChildProject. My question is how to resolve this.

I hope I made it clear what the problem is and will be really appreciative for any help.

Regards,

Petar

Btw: It is explained there How to link project in eclipse but I still have the error, that the child project cannot access resources accessed by the parent project.

Community
  • 1
  • 1
Petar
  • 2,241
  • 1
  • 24
  • 38

2 Answers2

3

Right click the Parent project and click properties. Then click Java Build Path on the left hand side. Next click the projects tab. Make sure the Child project is selected as a required project, if it is not Add the Child project.

You should also do this for the Parent Project.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • I am quite confused with what you have said. If you mean that I should add the child project as dependency for the parent project and the parent project as dependency for the child project, then I get a red question mark on both projects that indicate an error. Nevertheless, I still have the same error. As I said, I have already put the parent project as required project in the Java Build Path property section of the child project. – Petar Sep 28 '11 at 17:34
  • Your parent child analogy is confusing me. Basically you have one project that depends upon another. I think you may have wired the dependencies backwards. The project which does not need any classes from the other project, should be depended on by the other project. – Kevin Bowersox Sep 28 '11 at 18:00
  • " The project which does not need any classes from the other project, should be depended on by the other project" Yes this is exactly what I have done, but for some reason the resource folder cannot be accessed – Petar Sep 28 '11 at 18:20
  • I did this but I still don't see the source from Child project inside Parent process, and I get runtime `NoClassDefFound` errors. What to do? – Danijel Nov 13 '13 at 11:22
3

Although I am not entirely sure what you need to do, it sounds to me that you are trying to create circular dependencies, which is an anti-pattern. You want to avoid creating dependencies where project a depends on project b, but project b also depends on project a. If you provide more details on your use case and what you are trying to create I will be happy to provide some guidance as to how you could structure your dependencies.

Hope this helps.

TK Gospodinov
  • 8,302
  • 6
  • 24
  • 25
  • Okay I should explain it again, because the previous post brought some confusion. I have a project ProjectModel in the following directory : workspace/ProjectModel/src ... I have a folder in it where I keep resource files : workspace/ProjectModel/src/main/resources/ . The ProjectModel source build path contains : ProjectModel/src/main . I am accessing the files in the resources folder in Java by : "src\main\resources\". I am creating a ProjectView which should use the model project and provide a view for it. However, the view project cannot access "src\main\resources" folder of Model. – Petar Sep 28 '11 at 18:52
  • You should try to avoid loading resources directly. For loading resources from an Eclipse plug-in, see http://www.vogella.de/blog/2010/07/06/reading-resources-from-plugin/. For loading resources via a Classloader in a Java project, see http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html. – TK Gospodinov Sep 28 '11 at 20:59
  • Btw, using ClassLoader.getResourcesAsStream("path") works in Eclipse only if the "path" is included as referenced library in project. Correct me if it can be done otherwise. – Petar Sep 29 '11 at 10:12
  • Thank you very much. Although I did not understand the idea of the first link, the second link solved the problem. It is working well now. Поздрави ;) – Petar Sep 29 '11 at 10:16