testcontainers-java: Exception NoClassDefFoundError org/testcontainers/utility/PathUtils

Hello

There is an exception in MountableFile, it does not find the class PathUtils when it calls recursiveDeleteDir from shutdownHook. I think this is because during the shutdown hook the class loader is already closed by Maven and cannot be used to load new classes.

See attached stack trace

Exception in thread "Thread-17" java.lang.NoClassDefFoundError: org/testcontainers/utility/PathUtils$1
	at org.testcontainers.utility.PathUtils.recursiveDeleteDir(PathUtils.java:26)
	at org.testcontainers.utility.MountableFile.lambda$deleteOnExit$0(MountableFile.java:284)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ClassNotFoundException: org.testcontainers.utility.PathUtils$1
	at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
	... 3 more

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Comments: 34 (10 by maintainers)

Most upvoted comments

I just re-opened the issue, since it is clearly amplified by https://github.com/testcontainers/testcontainers-jooq-codegen-maven-plugin,

Hmm, I’m not really sure if we can help much with this. Running Testcontainers code directly from a Maven plugin isn’t something we’ve attempted to support, as we’re more focused on testing. I assume your usage scenarios are not testing?

As you say it does look like the Maven classloader closing is what’s causing it. If that’s Maven’s classloader behaviour then I imagine shutdown hooks are essentially not safe inside of a Maven plugin, and should be avoided.

If there’s something simple that we could change then I think we could accept a PR, but otherwise I’m afraid we might have to chalk this up as an unsupported use case. Sorry to disappoint.

Maybe I don’t understand something, but why then have this plugin in the first place? Plugin repo (also testcontainers) - https://github.com/testcontainers/testcontainers-jooq-codegen-maven-plugin The official how-to page also refers it: https://testcontainers.com/guides/working-with-jooq-flyway-using-testcontainers/

Looks like a big inconsistency to me.

As anybody can see in the comments here, this issue is not fixed and should be reopened. It was closed by some clean-up script because it was stale.

I use the latest version of testcontainers and can reproduce this issue on every Maven run:

src/test/application.properties:

spring.datasource.url=jdbc:tc:mariadb:10.11.2:///test?allowMultiQueries=true
spring.datasource.username=test
spring.datasource.password=test

pom.xml:

<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>1.19.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>mariadb</artifactId>
    <version>1.19.3</version>
</dependency>

Output of ‘./mvnw verify’:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  30.406 s
[INFO] Finished at: 2023-12-06T09:42:59+01:00
[INFO] ------------------------------------------------------------------------
Exception in thread "Thread-3" java.lang.NoClassDefFoundError: org/testcontainers/utility/PathUtils
	at org.testcontainers.utility.MountableFile.lambda$deleteOnExit$0(MountableFile.java:318)
	at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.lang.ClassNotFoundException: org.testcontainers.utility.PathUtils
	at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:593)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
	... 2 more

Steps to reproduce:

$ git clone git@github.com:komunumo/komunumo-server.git
$ cd komunumo-server
$ ./mvnw verify

I’m happy to provide more information if needed.

Did anyone ever find a solution to this issue? I also get the error with jOOQ+flyway.

Note, I only seem to get it when TESTCONTAINERS_RYUK_DISABLED=true, which is required on Bitbucket Pipelines.

@Auriium shutdown hooks are an essential part of JVM, and NoClassDefFoundError is caused by Maven, not Testcontainers.

However, you can call ResourceReaper.instance().stopAndRemoveContainer(...) after you finished with it. This way, it will be removed from the set of containers scheduled for removal, and the shutdown hook won’t load any classes.