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

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

问题描述

我是否可以使用ClassLoader实现从InputStream加载类?

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.

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

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