How to preload the entire jar file

I want to preload the whole jar file with all classes and resources at the beginning. So that access to the hard disk is not required for further execution of the Java program.

What's the recommended way to do this?


update:

The bank is taken over by the proguard, it discovers some dead code and removes it from the exit. This way, loading classes that will never be used are not a problem.

+3


source to share


2 answers


The recommended way is not to. This will create too many problems.

The problem is that (in general) you cannot guarantee to load all the classes that need to be loaded ... unless you parse in detail your code and the library code (including Java SE libraries) that it depends on. And this analysis is difficult due to the fact that some components can perform dynamic class loading.

In theory, you can traverse the class tree namespace and load each class. But you end up loading a huge amount of code that your application will never use.


Thinking outside the box ...



You can read the JAR file in memory and use a custom classloader to load from a copy in memory. However, there is a problem with this. Standard APIs JarFile

and ZipFile

allow you to read from a stream or buffer in memory. Therefore, you will need to use JarInputStream

to iterate through the JAR entries, read the corresponding content, and cache them in memory.

Of course, when loading your application class triggers that load the class from the standard libraries, your application must read from disk. (Unless you cache core library JARs and ZIP files.)


Then the thorny problem arises that some classes depend on dynamically loaded native libraries, and this dependency is not resolved when the class is loaded. These native libraries will also be on disk.

And there may be other resources in your application JAR file (images, prorty files, etc.) that can be dynamically read by the application.

+2


source


You can have a look at the Reflections library.

However, you have to think about the size and dimensions of the memory, it may happen that you are simply degrading the use of the hard disk.



Also, keep in mind the JRE / JDK classes, they are not present in the jar where the application classes are compiled and packaged.

-1


source







All Articles