2

I am developing a java application using Eclipse IDE, I have two projects SampleProject1 and SampleProject2, SampleProject2 depends on the SampleProject1. I found the two ways to add the dependency project. 1) Make a jar of SampleProject1 and include it in the SampleProject2 libs with the following classpath entry. 2) Export the source of SampleProject1 like below Please let me know if my understanding is not correct?

Queries: 1) Which is the best to add dependency project? 2) in the second approach, what is meant by "combineaccessrules="false"? Is it really required?

Jonah Graham
  • 7,890
  • 23
  • 55
M.S.Naidu
  • 2,239
  • 5
  • 32
  • 56
  • Are you using any kind of build tool? (e.g. Maven, Gradle) – Indrek Ots Oct 16 '15 at 06:24
  • Have not used Ant in a while, but you should be able to create a multi-module setup where one module depends on the other. Maybe this question and answer is helpful http://stackoverflow.com/q/2596746/2928051 – Indrek Ots Oct 16 '15 at 06:37
  • I dont think we should consider ANT, to answer my question, This is related about better way of including dependency. – M.S.Naidu Oct 16 '15 at 06:40
  • I think you already mentioned a good solution. Make a jar of SampleProject1 and include it in SampleProject2 libs. The only downside is that doing it manually can get cumbersome and error prone if you have more dependencies. This is were tools like Ant and Ivy, Maven, Gradle come in and automate it for you. Please correct me if I'm misunderstanding you in any way. – Indrek Ots Oct 16 '15 at 06:53
  • Yes @IndrekOts, You are correct, but instead maintaining jar for that we can pointout to the source – M.S.Naidu Oct 16 '15 at 09:08

1 Answers1

0

To tell Eclipse about the dependency, you can set the Java Build Path in SampleProject2 to require the SampleProject1.

Step-by-step:

  1. Right-click on the Java project (SampleProject2 in this case)
  2. choose Properties
  3. Within the properties, choose Java Build Path on the left hand side
  4. Select the Projects tab.
  5. Press Add...
  6. Select the dependent project (SampleProject1 in this case)
  7. Press OK until you have closed everything.
  8. It should now work for you.

Here is a screenshot of your destination for the above:

java build path

Example of it working

Take two projects named as you have them. Here is what I have on disk (excluding non java and .project files):

$ find * -name *.java -o -name .project
RemoteSystemsTempFiles/.project
SampleProject1/.project
SampleProject1/src/com/example/sampleproject1/UsefulClassNeededElsewhere.java
SampleProject2/.project
SampleProject2/src/com/example/sampleproject2/ClassThatNeedsSomethingUseful.java

ClassThatNeedsSomethingUseful:

package com.example.sampleproject2;

import com.example.sampleproject1.UsefulClassNeededElsewhere;

public class ClassThatNeedsSomethingUseful {
    private UsefulClassNeededElsewhere useful = new UsefulClassNeededElsewhere();

    public UsefulClassNeededElsewhere getUseful() {
        return useful;
    }
}

UsefulClassNeededElsewhere:

package com.example.sampleproject1;

public class UsefulClassNeededElsewhere {

}

And finally the dependency set up as described above. That should make everything work, and your workspace should look like this:

deps

As you can see, no error markers.

Things to keep in mind

You still need your build system to know about this dependency. i.e. your Ant file or other will need to know. Some build systems have excellent tooling in Eclipse, so that the dependencies will be calculated from the build system instead of manually replicated in the settings.

Jonah Graham
  • 7,890
  • 23
  • 55