Search code examples
javaclassclassloader

Use my class rather than from a JAR


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?


Solution

  • 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.