I need to copy a folder, packed in a Jar on runtime. I want to do it by calling a function in a class which is also contained in the same folder.
I've tried using getSystemResource
:
URL sourceDirUrl = ClassLoader.getSystemResource("sourceDirName");
File sourceDir = new File(sourceDirUrl.toURI());
but it doesn't work.
I probably have to use getResourceAsStream
function recursively. Is there a more elegant/strait-forward way to do this?
In case I have to do it recursively: 1. I don't want to specify the files hard-coded, I want to do it dynamically 2. I don't want to create a separate archive. I want this resource to be in the same Jar as the classes dealing with it
Thanks
I ended up doing what Koziołek suggested below. Although I was hoping for a more elegant solution, but it looks like this is as good as it gets.
Using the classloader you cannot retrieve a folder as it can not be a resource of your classpath.
Several solutions are possible:
I would preferably going for the second solution that is more portable and flexible but requires to repack the archive for all modifications of the folder content.
Jar is simple ZIP file. You can use java.util.zip.* package to decompress files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With