简体   繁体   中英

How do I get Java ClassLoader to output each class when loaded for the first time

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)

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
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM