How can I prevent the AntClassLoader and URLClassLoader from causing a linkage error?
Due to an older project, junit and hamcrest.core are put into ANT_HOME and our build uses includeantruntime=true in order to run unit tests.
I am now working on a project built with ant where I don't want to rely on any jar files in ANT_HOME. I religiously used includeantruntime=false in all javac stanzas.
However, now that I am using hamcrest for testing, I am getting a runtime linkage error.
java.lang.LinkageError: loader constraint violation: when resolving method
"org.junit.Assert.assertThat(Ljava/lang/Object;Lorg/hamcrest/Matcher;)V"
the class loader (instance of org/apache/tools/ant/loader/AntClassLoader5)
of the current class, myclass, and the class loader (instance of
java/net/URLClassLoader) for resolved class, org/junit/Assert, have different
Class objects for the type assertThat used in the signature
The runtime linkage error occurs while junit is running the tests.
I seem to have two choices, neither of which I like:
- Remove junit and hamcrest.core from ANT_HOME. This breaks the other project.
- Remove junit and hamcrest.core from the project and rely on the settings in ANT_HOME. This is what I am trying to avoid.
I would have thought that includeantruntime would have helped.
My goal would be to allow junit and hamcrest.core to remain in ANT_HOME while also including them in the project. That is, the contents of ANT_HOME should not matter.
Is there a solution to this problem?