3

The first opening of a new JInternalFrame in my Swing interface takes some time. According to the profiler, most of this time is spent in the Classloader.loadClass() method. I understand that it loads classes dynamically when needed and indeed the next time I open the same frame, it is much faster.

This application has a login screen, and then a time with an empty desktoppane before the user opens one of those internal frames. I figure that since I have some "down time", I could use it to preload some common classes in background, so that when the user actually opens an internal frame, it goes much faster.

Is there a way to achieve that, other than having to generate an entire path of "dummy contructors" through my panels and actually initialize an instance of those classes?

Ideally, if it could cascade such loading, it would be even better. A bit like the work done by classfileset in ant

Gnoupi
  • 4,715
  • 5
  • 34
  • 50
  • [ClassLoader#loadClass](http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#loadClass(java.lang.String)) – BackSlash Aug 07 '14 at 08:56
  • I did this issue with JasperReports. What I did was to create a dummy report preload its jars and dependencies – JCalcines Aug 07 '14 at 08:56
  • There is a [ClassPreloader](https://github.com/jermainexu/ClassPreloader) that you can use to customize your class preloading. For example load any class starts with some strings, regex or in some packages. – StarPinkER Oct 28 '14 at 13:09
  • @StarPinkER - interesting! Thanks for the link, I'll give it a look – Gnoupi Oct 29 '14 at 09:42

1 Answers1

4

Assuming you're not doing anything special with classloaders, you can call Class.forName(String className) for each class you want to pre-load.

This will execute static initializers in those classes, so it could affect the behaviour of your program (although I'd consider any such program to be badly-written).

user253751
  • 57,427
  • 7
  • 48
  • 90
  • Seems to do the trick indeed (no issues with static hopefully). Now to find a smart way to get the dependencies for some root classes... – Gnoupi Aug 07 '14 at 10:01