2

I have a project with different classes and packages as dependencies. Note that everything writte below occurs in one project.

I have a class that at some point runs the code getDiagramPanel().setRelationsPaintOrder(new Comparator() {.

getDiagramPanel() calls the method from DjtSheet.class, which is located in a dependency .jar-file. This method returns the DjtDiagramPanel object. I also have a DjtDiagramPanel.java file, which should override the one from the package and contains the method setRelationsPaintOrder().

In Java 7, this works fine. It correctly calls the method from the dependency, which returns the object in the format of the class which overrides the panelclass from the dependency package.

In Java 6 however, the panelclass from the dependency package is returned instead of the one from my project.

java.lang.NoSuchMethodError: com.dlsc.djt.gantt.DjtDiagramPanel.setRelationsPaintOrder(Ljava/util/Comparator;)V

Note that this message occurs at runtime! Compiling the project gives no errors.

How can I solve this?

Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • 1
    You have a different version of the jar at runtime and at compiletime. Check the runtime classpath. – Kayaman Feb 10 '14 at 08:47
  • How do you run you app? Do you use script or some kind of running framework? – AlexR Feb 10 '14 at 08:48
  • @Kayaman Eclipse shows me that `Java Compiler` setting is set to `JavaSE-1.6`. I've made sure this occurs by stting `maven-compiler-plugin` in my pom.xml to `1.6`. In the Debug Configurations, the JRE is se to `JavaSE-1.6` (jre6) as well (I must admit I hadn't done the latter yet even though I thought I did, but unfortunately the error persists). – Joetjah Feb 10 '14 at 08:53
  • @Joetjah It doesn't matter. You don't have a problem at compile time, so maven settings are alright. It's when you're running the program that you're using a wrong version of your jar. – Kayaman Feb 10 '14 at 08:54
  • @AlexR I compile and run the project using Eclipse. – Joetjah Feb 10 '14 at 08:54
  • @Kayaman That's true and probably the reason the DjtDiagramPanel class is overwritten by the one in the project. Shouldn't using that method return the class from my project rather than the one from my dependency (even though the object is returned through another class from the dependency)? – Joetjah Feb 10 '14 at 08:57

1 Answers1

-1

This problem definitely means that you have a problem in class path. I guess that the problem is that class DjtDiagramPanel is duplicate and you have 2 different veraions: one that has method setRelationsPaintOrder and second that does not have. Apparently you compile code against the "good" version and run against the "bad" one.

When this happens you can probably change the order of class loading by playing with order of dependencies in project properties of eclipse, but it will just fail later (on production). So, you should find what is the root cause of the duplication.

First find these 2 versions of the same class. Then find how the bad version arrived to your classpath. It typically happes because of 3rd party dependencies. If you are using maven you can use dependency plugin to find the root cause and disable it using tag "exclusion".

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • Thank you for your input. It's correct I've got 2 different versions. One is located in the dependency I'm using from a 3th party publisher, the other in my project. I need the one from my project to override the one from the dependency. This seems to work in Java 7, but not when running in Java 6. What do you mean exactly with 'finding the root cause'? I don't believe I can exclude a specific class in a dependency, can I? – Joetjah Feb 10 '14 at 10:06