Load/read/use jar file via web

Asked

Viewed 199 times

2

It is possible to load/read/use a web jar without having to download it and use it in the classpath?

If it’s possible how I do it?

1 answer

1


It is possible, but it is a complex and delicate task, not recommended for most cases, and it has several limitations.

Basically, you can load new classes manually, for example, through the method loadClass of a ClassLoader.

The problem is that within a Java web application there is usually a hierarchy of Classloaders and how you will load classes depends on various factors and how they will be used.

It is possible to use the most specialized class URLClassLoader, which allows specifying Jars and directories in the constructor, and the class will be automatically located when you call the method loadClass or equivalent.

Create a new ClassLoader allows you to isolate the loaded classes, which cannot access the normal system classes. This may be desirable or not. An advantage of using a new ClassLoader is that if it is no longer used and collected by Garbage Collector, loaded classes will be deleted as well.

On the other hand, if you want to load the classes as part of your program, you can try using ClassLoader of your web application. However, in this case, the class will never be downloaded from memory (at least in a regular JVM).

An alternative to this is to use the Osgi standard, in which you can load and download modules on-the-fly.

In short, it is complex and can lead to serious problems. It would be interesting to know exactly the goal of doing this to determine if it is really necessary, the most appropriate method and if the effort is worth it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.