将类从一个jar文件从一个url加载到内存,然后运行所有类中的主类 [英] Load classes from a jar file from a url to memory and then run the main class out of all the classes

查看:199
本文介绍了将类从一个jar文件从一个url加载到内存,然后运行所有类中的主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个jar文件从一个URL加载到内存中的类,然后运行其中一个类加载。我不想让类文件或jar永远是用户可以访问的。我已经看过一些类似的问题,但他们没有回答。

I'm trying to load classes from a jar file from a URL into memory then run one of the classes loaded. I don't want the class files or the jar to ever be accessible by the user. I've seen a couple similar questions, but they have gone unanswered.

我知道它可能会使用URLClassLoader和ByteArrayInputStream / ByteArrayOutputStream。

I know it will probably use URLClassLoader and ByteArrayInputStream/ByteArrayOutputStream.

感谢您提供任何帮助。

类似的未回答的问题:

如何从URL加载jar而不下载它?

从URL加载jar

推荐答案

你在正确的轨道上(我想)。步骤如下:

You are on the right track (I think). The steps would be:


  1. 使用URLConnection打开JAR文件的流。


  2. 在字节数组上打开一个ByteArrayInputStream,然后将其包装在JarInputStream中。


  3. 编写一个自定义类加载器,它使用内存中的缓存来存储内存中的内容。 JAR文件条目和内容。

  1. Use URLConnection to open a stream to the JAR file.
  2. Read the stream and write it to ByteArrayOutputStream, and extract the byte array.
  3. Open a ByteArrayInputStream on the byte array, then wrap that in a JarInputStream.
  4. Iterate through the members of the entries of the JAR file, saving the entry information and buffering the entry file content in memory.
  5. Write a custom class loader that uses the in-memory cache of the JAR file entries and content.

我无法指向示例代码,因为我找不到任何。

I can't point you at example code because I couldn't find any.

(你必须在内存中缓存内容,因为 JarFile API需要一个 RandomAccessFile ,这意味着数据在文件系统中在一些系统上,你可以创建一个临时文件,打开一个RAF,取消链接,然后获取并写入URL到RAF。不可移植...)

(You pretty much have to cache stuff in memory since the JarFile API requires a RandomAccessFile and that implies that the data is in the file-system. On some systems you could create a temporary file, open a RAF on it, unlink it, and then fetch and write the URL into the RAF. But that isn't portable ...)

我应该指出,如果你这样做是一些许可的一部分, 知识产权管理计划,你可能浪费你的时间。任何您想在客户端代码(即在用户的计算机上运行的代码)上实现的实用方案都可能被破坏,除非该计算机完全锁定。

I should point out that if you are doing this as part of some licensing or "intellectual property management" scheme, you are probably wasting your time. Any practical scheme that you care to implement in client-side code (i.e. code that runs on the user's machine) can be broken ... unless the machine is totally locked down.

这篇关于将类从一个jar文件从一个url加载到内存,然后运行所有类中的主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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