4

what

I would like to use jdk.internal.loader.ClassLoaders$AppClassLoader instead of spring-boots org.springframework.boot.loader.LaunchedURLClassLoader. However, I am uncertain of the implications of how this will affect the spring-boot runtime.

why

We recently ran into a java.lang.InstantiationError when making Spring repository calls inside java's parallelStream(). I learned that threads in the parallel stream are using the AppClassLoader and thus are unable to find the application classes residing in LaunchedURLClassLoader, a child of AppClassLoader in the classloader hierarchy.

how

The spring-boot documentation shows you can run an unpacked spring-boot jar without using their classloader. This conveniently means that application classes are also loaded into AppClassLoader, resolving our issue with the parallelStream().

$ java -cp BOOT-INF/classes:BOOT-INF/lib/* com.example.MyApplication

NB: This is also how Intellij IDEA runs spring-boot applications.

question

Who knows if the spring-boot classloader is doing more than enabling executable jar and war files that I should know about. I found the documentation not explicit enough. Is it safe to not use spring-boot classloader in production?

Community
  • 1
  • 1
flanders
  • 41
  • 3
  • Don't. You are fixing the problem in the wrong way. Also you should also be cautious when using `parallelStream` as it might block quite easily your whole system. – M. Deinum Nov 07 '19 at 10:22
  • @M.Deinum, Thank you for pointing that out, I agree. This will be done in time. I would still like someone to answer my question about spring-boot classloading. – flanders Nov 07 '19 at 10:50
  • I sincerly doubt the issue is related to it, although it might solve it imho it is a symptom of something else. – M. Deinum Nov 07 '19 at 11:11
  • @M.Deinum, Would you expect parallelStream() threads (from the ForkJoinPool) to use the spring-boot classloader, but something else is preventing that? Can you elaborate on that? – flanders Nov 07 '19 at 14:17
  • 1
    I would expect the correct classloader to be used (the current one, and reset afterwards). There have been some bugs (like [this](https://bugs.openjdk.java.net/browse/JDK-8172726) and others linked in that one) not properly setting or cleaning up the classloader. So either you are running with an old JDK without the patches, or you are doing things in your code you shouldn't be doing. In general it should just work. – M. Deinum Nov 11 '19 at 06:41

0 Answers0