How do I get Java ClassLoader to output each class as loaded for the first time ?
Is there a java
option that can do this or if I subclass ClassLoader and simply add a System.out.println() to it before callaing super() how would I make my application use my Classloader ?
Why do I want to do this ?
When running my application with java 9 it falls over with an error, but the stack trace does not originate in my code so I dont know what is causing it. My idea was to output each class as loaded and then I would have a better idea of where things are going wrong.
Uncaught error fetching image:
java.lang.NullPointerException
at java.desktop/sun.awt.image.URLImageSource.getConnection(Unknown Source)
at java.desktop/sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at java.desktop/sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at java.desktop/sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at java.desktop/sun.awt.image.ImageFetcher.run(Unknown Source)
To know the ClassLoader that loads a class the getClassLoader() method is used. All classes are loaded based on their names and if any of these classes are not found then it returns a NoClassDefFoundError or ClassNotFoundException.
A Java Class is stored in the form of byte code in a . class file after it is compiled. The ClassLoader loads the class of the Java program into memory when it is required. The ClassLoader is hierarchical and so if there is a request to load a class, it is delegated to the parent class loader.
Initially when a JVM starts up, nothing is loaded into it. The class file of the program being executed is loaded first and then other classes and interfaces are loaded as they get referenced in the bytecode being executed.
You can use the -Xlog
option with the tags class
and load
. Like this:
java -Xlog:class+load <other arguments>
The output would be something like this:
...
[0.083s][info][class,load] jdk.internal.module.ModuleHashes$Builder source: jrt:/java.base
[0.084s][info][class,load] java.util.Collections$UnmodifiableMap source: jrt:/java.base
[0.084s][info][class,load] jdk.internal.module.ModuleResolution source: jrt:/java.base
[0.084s][info][class,load] java.lang.module.ModuleReference source: jrt:/java.base
[0.084s][info][class,load] java.util.function.Supplier source: jrt:/java.base
[0.084s][info][class,load] jdk.internal.module.SystemModuleFinder$1 source: jrt:/java.base
[0.084s][info][class,load] jdk.internal.module.ModuleReferenceImpl source: jrt:/java.base
[0.084s][info][class,load] java.util.KeyValueHolder source: jrt:/java.base
[0.084s][info][class,load] jdk.internal.module.ModuleHashes$HashSupplier source: jrt:/java.base
[0.084s][info][class,load] jdk.internal.module.SystemModuleFinder$2 source: jrt:/java.base
[0.085s][info][class,load] jdk.internal.module.ModuleBootstrap$PerfCounters source: jrt:/java.base
[0.085s][info][class,load] java.util.Optional source: jrt:/java.base
[0.085s][info][class,load] jdk.internal.loader.BootLoader source: jrt:/java.base
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With