Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#670·byte-buddy

Proper way to repackage ByteBuddy into the library

Author: qwwdfsadCreated Jun 19, 2019Updated Dec 20, 2022
Labelsenhancementquestion

Some context:

We at kotlinx.coroutines have a special "debug" library that uses ByteByddy to redefine some classes for the sake of better user experience.

Our API shape has two modes:

  1. Programmatic API using self-attach mechanism. Everything works fine, a user has our jar (and thus ByteBuddy) in the classpath.
  2. Attach mechanism. If a user already has its own application without our library in the classpath, we don't want to force them to recompile their project. So to make it as simple as java -jar myapp.jar -javaagent:kotlinx-coroutines-debug.jar we'll have to shade ByteBuddy into our library. But then we can mess up with other libraries that use ByteBuddy, especially in case of incompatible versions. To avoid that, we not only shade but also repackage ByteBuddy.

It works fine most of the time but fails with java.lang.UnsatisfiedLinkError: Native Library /.../jre/lib/amd64/libattach.so already loaded in another classloader when the target application uses libraries that also use ByteBuddy (e.g. Spring or Mockito).

So the question is, what is the best way to have a self-contained JAR suitable for attach and that also works with other libraries that use ByteBuddy?

Source: raphw/byte-buddy

View original on GitHubView discussion on GitHub