2

(Eclipse Luna)

Say I have the following "Project A":

Project A
    a.b.c.package1
        One.java
        Two.java
        Three.java
    a.b.c.package2
        Four.java
        Five.java

And I have another "Project B" that attempts to serve as an experimentation project that uses Project A as a dependency. So Project B depends upon Project A in the project settings. Project B looks like this:

Project B
    x.y.z.package3
        Main.java

(which then uses many of the classes in project A, and Main.java contains application main() routine).

This of course works fine. However, what if I want project B to experiment with changing a class from project A, and have it replace it entirely. Such as:

Project B
    x.y.z.package3
        Main.java
    a.b.c.package2
        Four.java      <-----this is to replace the Four.java in Project A

....and have Project A's Four.java be completely ignored by everything so long as I'm "in" Project B. So if Five.java refers to Four.java, it actually is referring to Project B's Four, etc.

Can this be done?

  • This is not an eclipse specific problem. Overriding whole classes rely on the mechanism of how the classes are ordered in the classpath and loaded at runtime. You would need to write your own classloader... – noone Dec 06 '14 at 13:50
  • Why do you do this? I mean what are you trying to do. – outdev Dec 06 '14 at 14:01
  • @noone, no not the classloader specifically. And this *is* an eclipse issue: The relationship between classes is still under control of eclipse, not the underlying JDK. The JDK need not know what is in what package at all outside of what eclipse hands to it. And it's not the concept of a directory, because some IDE's don't use the nominal "directory is the package" at all. I was able to accomplish such "overriding" similar to this in Visual Cafe for Java in the late 90's. –  Dec 06 '14 at 15:18
  • @stackdev, I would like to have entire permutations of projects exist as other projects in their own right, but only as testing vehicles to break in new algorithms. It would be valuable for me to be able to say "this project is just like the other project, but I'm changing the behavior of class *X* entirely. –  Dec 06 '14 at 15:19
  • If I move the design of my projects over to more of a behavior centric (interface heavy) over class heavy style, then I can have new classes entirely in Project B that behave like the classes I'm replacing. But there's a class inheritance hierarchy I just don't want to mess with currently. –  Dec 06 '14 at 15:23
  • 1
    I am trying to understand...If Project B's Five.java refers to Four.java, what do you want to happen. – outdev Dec 06 '14 at 15:26
  • You mean Project A? There is no ProjectB Five.java. Project B is attempting to only supply Four. –  Dec 06 '14 at 15:29
  • 1
    I mean Project B...I copy pasted your last sentence in the question. – outdev Dec 06 '14 at 15:33
  • If Project A's Five.java refers to Four.java, then that referral would end up being Project B's Four.java (my goal anyway). As if I had copies project A over entirely to Project B (with no dependency) and altered Four.java in Project B. –  Dec 06 '14 at 15:33
  • @tgm1024 The relationship between classes is under control of eclipse? So how does a customer run your application without eclipse? Eclipse just compiles your code via `javac` and runs it via `java` plus a certain classpath. You can do the same completely without eclipse, so it's not an eclipse specific problem. – noone Dec 06 '14 at 15:35
  • @noone, perhaps I'm giving making assumptions about what eclipse is actually doing then. Are you saying that eclipse is leaving the directory structure as is and merely telling the JRE to execute it in place (with the concept of directorys being packages, etc.?) –  Dec 06 '14 at 15:38
  • To answer your question (based upon my assumption), I had assumed that eclipse controls WHICH Four.java is being used in any project context. Having people run outside of eclipse isn't an issue so long as everything is in the right place. They would not be able to run my Project B (as described) without eclipse, or perhaps without me building a jar file with the right contents, etc. –  Dec 06 '14 at 15:43

1 Answers1

2

So you will have two Four.class files in your classpath. When jvm needs a class it will use first it can find, if multiple classes with same fully qualified name on the classpath. So if you want Project A's Five.java to refer Project B' Four.java, you have to make Project B classes to be first in the classpath.
In eclipse go to

Project B Properties > Build Path > "Order and Export"

enter image description here

Use Up/Down buttons to move Project B above Project A, that way ProjectB will be first in the claspath.

outdev
  • 5,249
  • 3
  • 21
  • 38
  • Ok, thanks SO MUCH for your help. I'm a bit confused about this though, because now it seems to be working without doing anything at all to the order&export panel. Also, I don't see what you're seeing (B/src), I just see project names. Further confusion: How come this is working now and it didn't previously? I tested all of this before asking my question. Had it simply "worked" with the B/Four overriding the A/Four I wouldn't have come here in the first place. This is behaving how VisualCafe seems to IIRC, which is ideal. –  Dec 06 '14 at 16:49
  • 1
    @tgm1024 The way I understood it you want to override a class from Project A in Project B. Project B should always be first in the classpath by default (the classpath of project B, that is). Project A should come after that. That's why you actually shouldn't need to change anything. – noone Dec 06 '14 at 16:58
  • @noone, yeah, I was operating under that assumption, but got an error message, hence me showing up here in the first place. Something akin to "expecting a.b.c.Four and got a.b.c.Four", but more in type mismatch speak. I honestly don't understand this. I was similarly goofed up by a dependency project required to be actively open to be seen, but this isn't that. I'm REALLY uncomfortable with things that seem nondeterministic, even if it is in reality a cockpit error. –  Dec 06 '14 at 17:16
  • Gentlemen, don't get me wrong: I'm assuming this is my fault. I just don't like not understanding it, and I can't seem to replicate the mistake either. –  Dec 06 '14 at 17:17
  • 1
    I created two projects with names "A" and "B". The "Order and Export" view display project, and its dependencies, as project names. Their order correspond to their order in the classpath at runtime. By default "B" was first, and "A" was second, (dependencies always follow). Its interesting how to manage to get default behavior like they are swapped. – outdev Dec 06 '14 at 20:04
  • @ultracoms, I appear to be cursed with IDEs. I am now evaluating Netbeans and am having the exact same problem. If you have any ideas about how to help, I've described it here in very *very* similar wording. http://stackoverflow.com/questions/29574103/in-netbeans-8-0-2-how-to-have-a-project-replace-a-class-in-a-different-dependen –  Apr 12 '15 at 14:10
  • Call it hopelessly old and unfeatured, but I long for the simplicity of Visual Cafe for Java circa 1999. –  Apr 12 '15 at 14:11