0

I have a class com.example.TestClass which is in a JAR archive. Now, in my project (which uses the aforementioned JAR) I want to use a modified version of the class.

What I have tried so far was to create the very same class and package in my project's src directory, because I was hoping that it would be used during class loading, but unfortunately, that didn't work.

I have read about Javaassist, but I only want to restort to it unless absolutely necessary.

Do you have any suggestions?

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
Pateman
  • 2,727
  • 3
  • 28
  • 43
  • well you can certainly create the class but in a different package, or create a subclass of the same or even create jar with updated code and version number – Lalit Mehra Oct 19 '15 at 11:59
  • Subclassing is not an option, because I want to basically use my version of the test class, even in the JAR. – Pateman Oct 19 '15 at 12:01
  • 1
    Are you using Eclipse? Check the order of the imports in your project. – cassiomolin Oct 19 '15 at 12:03
  • Seen this: [How to load Different versions of the same class](http://stackoverflow.com/questions/11759414/java-how-to-load-different-versions-of-the-same-class/11759613#11759613) ? – pelumi Oct 19 '15 at 12:19
  • Let me understand your problem.You have 2 versions of the same class in your classpath and you want to load a particular version among them. Is this you require ? – Tom Sebastian Oct 19 '15 at 12:19
  • @TomSebastian, exactly. – Pateman Oct 19 '15 at 12:21
  • you can refer https://dzone.com/articles/java-classloader-handling – Tom Sebastian Oct 19 '15 at 12:29

2 Answers2

1

It seems to be an issue with your class path order.

Changing the classpath order in Eclipse

  • Go into your Project Properties > Build Path.
  • In the Order and Export tab, find your src folder and press the Up button to move it higher up in the class path order. Keep pressing Up until it is above your JAR.
  • Press OK.

Changing the classpath order in Intellij IDEA

  • Open Project Structure (either Ctrl + Alt + Shift + S or File > Project Structure).
  • Select Modules on the left, under Project Settings.
  • Select the module in question in the middle pane.
  • Arrange the order of the dependencies lists. Be sure your Module Source is above the other dependencies.
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • 1
    I had the right order, but had to move the source below my libs, then move it again above the libraries and rebuild everything. Thanks, that worked! – Pateman Oct 19 '15 at 12:27
0

Maybe you could make sure that you TestClass is first on the classpath. Take a look here: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

TamasGyorfi
  • 587
  • 1
  • 6
  • 14