I checked the source code of JarClassLoader
and the problem is that JarClassLoader also has a shutdown hook to close the jar file. And as the documentation of shutdown hooks says, they may be invoked in any order, or even in parallel. So when your hook "gets in" before JarClassLoader
s, your code works. When it's a bit too late, you get this exception.
A way to get around this would be to ensure the PrintWriter
class is loaded before invoking the shutdown hook.