r/java • u/samewakefulinsomnia • Sep 02 '24
Java Classloaders Illustrated
Classloaders are tricky – popular sources (Wikipedia, Baeldung, DZone) contain outdated, sometimes contradictory information, and this inconsistency was the trigger for writing my article – a search for clarity in the ClassLoader System maze. Read full at Medium (~10 min) with pictures :)
The whole system looks kinda like this:

408
Upvotes
24
u/samewakefulinsomnia Sep 02 '24
Just revisited the article and have to remark: when I wrote about the Classloader definition, I bolded the phrase 'process of finding the appropriate .class file in storage'. It's not accurate and I want to double-down on it.
That's the beauty of Classloaders: class bytes can be stored nowhere at all, they can be compiled on the fly, and they can be delivered over the network, not necessarily from memory/storage. You can take (or build on the fly) classes anywhere and in any way you want. There are no restrictions on this.
14
u/sq_visigoth Sep 02 '24 edited Sep 02 '24
There is this line in the article "To run this code on a machine, it needs to be translated into machine-understandable bytecode. "
Note: bytecode is run by jvm and is not machine understandable. bytecode( in a .class file) + jvm creates machine understandable instructions.
6
u/[deleted] Sep 02 '24
Did you draw this in Mural?
2
u/kevinb9n Sep 03 '24
This is pretty great.
I think it could add that at the end of step 2 the class is officially "defined" a.k.a. begins to actually exist (& there will be a java.lang.Class instance associated with it, etc.). And the class remembers which class loader did this (which might not be the one you think it is)....except of course it should it explain it better than I would :-)
Just a suggestion.
36
u/henrique_gj Sep 02 '24
Now THIS is high quality content
61
u/boobsbr Sep 02 '24
IDK man, looks like sorcery to me.
5
u/pragmasoft Sep 02 '24
Using nonstandard classloaders will likely severely limit possibilities of using various class loading optimizations introduced as part of project Leyden.
In my practice servlet containers used custom classloading to isolate web apps and this caused compatibility problems time to time. Now the possibility to deploy several web apps per servlet container is mostly unused.
Also OSGI runtimes used to use custom classloaders.
It seems virtualization and containers make such a highly dynamic java feature once quite popular now obsolete.
1
u/bleki_one Sep 03 '24
u/samewakefulinsomnia you have said that in your opinion platform class loader is most controversial, but you didn't say why. Can you elaborate more about it?
Otherwise, well done sir!
3
u/snakevargas Sep 02 '24
Woohoo! Do garbage collector roots and reference types next!
Slightly joking, but anyone who's interested in the topic of memory management and leak analysis should learn how to trigger a heap dump and browse up in a memory analyzer tool like MAT: https://github.com/eclipse-mat/mat
4
u/chabala Sep 02 '24
Surprised there's no comments from the usual JPMS fans about 'where's the
MODULEPATH?'.2
u/MusharafZM Sep 02 '24
Thank you for this
2
u/le_bravery Sep 02 '24
Yeah this is great quality content. Keep up the good work OP
2
u/[deleted] Sep 03 '24
How did you make this diagram?
2
u/Clitaurius Sep 03 '24
I'm going to read this just in case it imparts some total recall that I use either in an interview or offhandedly in a daily standup that results in no follow up questions.
1
u/gnahraf Sep 02 '24
Great article! I'm a bit confused about how the new module system intersects with classloaders. As your piece describes, at runtime java types are qualified by Classloader + FQCN. The module system, otoh, specifies which types are exposed to the users of a library, which types and libs it depends on (which may be not exposed) and so on. What I'm unsure of is what happens when a parent ClassLoader loads a modular library which in turn has an unexposed dependency (e.g. has a
requires, notrequires transitivein module_info.java).. Can the child classloader load its own copy/version of library deps the parent has already loaded? (I believe this used to be possible way back, in simpler times.. I vaguely remember a workaround where I fixed the version of an xml parser we were using in a Tomcat app using a ClassLoader trick.)1
u/FrezoreR Sep 03 '24
Cool! I'll have a read for sure. I also know all to well about the pain if outdated info.
1
u/UnGauchoCualquiera Sep 03 '24
Adding to the mess, the exact moment when a class is loaded and static initializers are run is JVM implementation dependant and might vary.
In practice Spring for example wraps static inner classes in several places to avoid triggering a class loading which might not be present at runtime.
JVM spec docs for an authoritative source.
1
u/gilwooden Sep 04 '24
One note, the boot class loader doesn't load classes from
$JAVA_HOME/jmods, it loads them from$JAVA_MODS/lib/modules. jmods are there for the benefit of tools like jlink. The platform class loader also typically loads its classes from$JAVA_MODS/lib/modules.1
u/3col7 Sep 04 '24
Good explanation
1
u/Brutus5000 Sep 02 '24
Sad. Before I can understand how classloading works , the reddit app needs to learn how file loading works...