作为 InputStream 从 JAR 加载? [英] Loading from JAR as an InputStream?

查看:24
本文介绍了作为 InputStream 从 JAR 加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可用于从 InputStream 加载类的 ClassLoader 实现?

Is there a ClassLoader implementation I can use to load classes from an InputStream?

我正在尝试将具有 InputStream 的 JAR 加载到新的 ClassLoader 中.

I'm trying to load a JAR for which I have an InputStream into a new ClassLoader.

推荐答案

这不太可能,因为如果您尝试自己动手,就会发现.您将无法随机访问 InputStream 来查找请求的类,因此您必须将内容缓存在内存或文件系统中.

This is unlikely, as you will find if you try to do it yourself. You won't be able to randomly access an InputStream to look up classes as they're requested, so you'll have to cache the contents either in memory or in the file system.

如果缓存在磁盘上,只需使用 URLClassLoader.

If you cache on disk, just use URLClassLoader.

如果缓存在内存中,则需要使用 JarInputStream 然后扩展 ClassLoader (覆盖适当的方法).这种方法的缺点是您不必要地将数据保存在 RAM 中.

If you cache in memory, you'll need to create some sort of Map with JarInputStream and then extend ClassLoader (overriding the appropriate methods). The downside of this approach is that you keep data in RAM unnecessarily.

这篇关于作为 InputStream 从 JAR 加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆