Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean up after external Java library

Tags:

java

jar

I'm using some external, i.e. jar, library in my application which unfortunately does not properly clean up after itself, i.e. the second time I create an object from this library it does not work as expected. I suspect that it is because the library creates some threads which keep running, but I'm not sure.

Is there any way to force a complete clean up of the mess that external library creates?

like image 881
Demiurg Avatar asked Feb 12 '26 22:02

Demiurg


1 Answers

There might be reasons unrelated to threads. For example, static class initializers only run once, when the class is loaded for the first time. So something like this may be the cause:

public class StupidSingleton {
  private static StupidSingleton instance;

  public StupidSingleton() {
    if (instance == null) {
      instance = this;
    } else {
      instance.foo(); // the implementation is irrelevant in this context
    }
  }
}

Whatever it is, there are three basic solutions available:

  1. Don't use the jar. Look for updates and alternatives.
  2. Control the classloading with custom ClassLoader. It's a hard road.
  3. Fix it. Even with no source you can use a debugger and decompile the classes. If you find the cause and can't get an official fix, you might still create proprietary_fixed.jar from the decompiled classes.
like image 168
lokori Avatar answered Feb 14 '26 10:02

lokori



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!