简体   繁体   English

如何确保类加载器可以访问临时jar文件

[英]how to make sure that the temporary jar file is accessible by the classloader

I have a custom jar class loader which utilizes URLCLassLoader. 我有一个使用URLCLassLoader的自定义jar类加载器。 I have to read the jar contents from database and load them. 我必须从数据库读取jar内容并加载它们。 There are several questions related to this. 有几个与此有关的问题。

  1. Can I somehow load all the classes from the byte stream instead of first saving the jar contents from database to a temporary file and the providing the file:// URL to the URL class loader? 我可以以某种方式从字节流中加载所有类,而不是先从数据库中将jar内容保存到临时文件,然后将file:// URL提供给URL类加载器吗?

  2. if the answer to above question is yes, then will the classloader make sure that it reads all the class files and resources from the byte-stream at once to avoid ClassNotFoundError later. 如果对上述问题的回答是“是”,则类加载器将确保立即从字节流中读取所有类文件和资源,以避免以后发生ClassNotFoundError。

  3. if the answer to Q1. 如果答案是Q1。 is no. 没有。 Then going by my method, I have created a temporary .jar from the database byte stream and given the file:// URL to the URLClassloader. 然后按照我的方法,我从数据库字节流中创建了一个临时 .jar,并为URLClassloader指定了file:// URL。 Does URLClassloader makes sure that the file on that url, being temporary, is not destroyed till it is being used so as to avoid ClassNotFoundException? URLClassloader是否确保该URL上的临时文件在使用之前不会被破坏,以避免ClassNotFoundException?

Any other solution to the above overall problem is also welcome. 也欢迎对上述总体问题有任何其他解决方案。 Thanks in advance. 提前致谢。

You can construct a URL with your own URLStreamHandler . 您可以使用自己的URLStreamHandler构造一个URL However, normal implementation of URLClassLoader will save the contents to a temporary file and use the JarFile API. 但是, URLClassLoader常规实现会将内容保存到临时文件并使用JarFile API。

If you create a temporary file, on a UNIX system you should be able to delete after opening. 如果创建一个临时文件,则在UNIX系统上,您应该能够在打开后将其删除。 On Windows you'll need to keep it around until you have finished with the class loader. 在Windows上,您需要保留它,直到完成类加载器为止。 From Java SE 7 you can close a URLClassLoader . 在Java SE 7中,您可以关闭URLClassLoader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM