Is it possible to load classes from a JAR stored in memory?

N

nmistry

Does anyone know if it is possible to load classes directly from a jar
file that is stored only in memory?

The reason for my inquiry, is b/c I have a collection of class files
that get loaded from a custom classloader, and im trying to find an
easy way to bundle and transport them. I especially would like to avoid
writing them out to disk.

I figure that i could load the jar into memory, expand it somehow in
memory, and then iterate over the class files and load them
individually. I also figure that there is probably a better way.

Thanks in advance.

-N
 
J

John C. Bollinger

Does anyone know if it is possible to load classes directly from a jar
file that is stored only in memory?

The reason for my inquiry, is b/c I have a collection of class files
that get loaded from a custom classloader, and im trying to find an
easy way to bundle and transport them. I especially would like to avoid
writing them out to disk.

So you are loading them over the network? Creating them on the fly?
They have to come from /somewhere/. Wherever "somewhere" is, that's
where your custom ClassLoader can get the bytes; they don't need to come
in Jar format.
I figure that i could load the jar into memory, expand it somehow in
memory

Again, from where? Network? If you have the Jar available over the
network then you can probably use a plain old URLClassLoader to load the
classes therein, with no need for anything special. Of course, your
application then fails if the server is unavailable. Also, the Jar
itself would not be very much better protected than if you just shipped
it with the application.
, and then iterate over the class files and load them
individually. I also figure that there is probably a better way.

If, for reasons that are opaque to me, you constructed a byte[]
containing the bytes of a Jar file, then you could read it in-memory by
constructing a ByteArrayInputSream around it and a JarInputStream around
that. It would be possible to construct a custom ClassLoader that could
use that technique to load classes from the byte[]. I'm not currently
seeing anything to be gained by that, however.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top