简体   繁体   中英

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

I have a custom jar class loader which utilizes URLCLassLoader. I have to read the jar contents from database and load them. 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?

  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.

  3. if the answer to 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. 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?

Any other solution to the above overall problem is also welcome. Thanks in advance.

You can construct a URL with your own URLStreamHandler . However, normal implementation of URLClassLoader will save the contents to a temporary file and use the JarFile API.

If you create a temporary file, on a UNIX system you should be able to delete after opening. On Windows you'll need to keep it around until you have finished with the class loader. From Java SE 7 you can close a URLClassLoader .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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