Search code examples
javajarclassloader

Java override class to Load project Class instead Jar class


I have a jar that contain a class that I would like to modify. For some reason I do not want to re-compile and update the Jar. So what I do is I recreate a full qualified name for that class in my project, and place my code in it, but I do not have an idea how to ensure JVM loaded my project class and not the Jar class. I have look through some post but they are about how to load a Jar class into project, not the other way round.


Solution

  • Typically all you need to do is ensure that your class is on the classpath before the jar containing the code you're trying to override.

    If you're running from the command line (replace : with ; for execution on windows):

    java -cp myproject.jar:their.jar com.package.Main
    

    If you're using Eclipse right-click on the project > Build Path > Configure Build Path..., then select the "Order and Export" tab and make sure your project source directory is above the jar you're overriding in the list.