0

I have folder in class path which contains only resources file's that are to be accessed. I cannot give that files from file system. What I need is after deploying spring boot jar with command

Java -jar somename.jar

I want to create or delete files inside my resource folder in the jar

kushma gonna
  • 236
  • 3
  • 19
  • 1
    This is a bad idea. Jar files aren't meant to be modified by the code they contain. Why do you think you need to delete those files? – CryptoFool Nov 12 '20 at 19:40
  • Does this answer your question? [How to delete a specific File/Folder from a jar pragmatically in java](https://stackoverflow.com/questions/25440540/how-to-delete-a-specific-file-folder-from-a-jar-pragmatically-in-java) – Brian McCutchon Nov 12 '20 at 19:42
  • Also https://stackoverflow.com/questions/4520822/is-there-a-quick-way-to-delete-a-file-from-a-jar-war-without-having-to-extract – Brian McCutchon Nov 12 '20 at 19:43
  • Neither of these address the situation where the code that will do the deletion is running from the target jar file. It also doesn't address if it's a good idea to do this thing regardless of how it would be done. (or am I misunderstanding the question?) – CryptoFool Nov 12 '20 at 19:45
  • @Steve I have some xml file in class path I need to create a new xml or delete existing one. I don't want modify any class files. I need to provide only class path as input to some method in class – kushma gonna Nov 12 '20 at 19:48
  • You shouldn't have to modify anything inside the jar file. Is this because you don't have any local storage and so the jar is the only place these files can live? What you'd normally do is use the files from the jar file to initialize an area on the filesystem. once that area exists, you just ignore the files in the jar. – CryptoFool Nov 12 '20 at 19:51
  • unless your case is very unusual, you shouldn't need to modify the jar file. I've never heard of any app working this way, and I've been around a long time. A jar file should be seen as an immutable resource. – CryptoFool Nov 12 '20 at 19:52
  • @Steve no Steve I have a java class which consists a method which takes xml files only from class path. This class is an external dependency not written by me – kushma gonna Nov 12 '20 at 19:53
  • Then add an external directory to your classpath - or maybe your case is super special and you know what you're doing. Just know that there are many apps that follow this pattern. They define an external directory that is on the classpath, and they initialize that location with files that are in their jar file. Configuration often works this way...the app will lay down default config files from the jar, but then the operator can go and change those files on disk. – CryptoFool Nov 12 '20 at 19:54
  • After creating jar I have package "todo" inside it contains classes and I have another folder like "features" it consists of xml files can I create or delete files inside it – kushma gonna Nov 12 '20 at 19:58
  • I get what you're saying. I've just never heard of a jar file being considered a writeable storage location. You can try to modify it like any other .zip file, but I don't know if the Java class loader that has it open is going to like that. Or if the operation is going to be allowed given that the JVM has it open. - good luck with that. – CryptoFool Nov 12 '20 at 20:00
  • The best way is if you are already constructing your classpath such that you can add another directory to it, before running your app. Your code can either know what that path is, or you can put something in the directory to search for on the classpath to locate the directory, like a subdirectory that you know the name of. I'm not an expert at this. What you might want to do is post a new question and describe your situation completely, like you have in the comments. This can be complex any way you go. Someone should know just how to handle your situation...possibly by editing your jar. – CryptoFool Nov 12 '20 at 20:22

0 Answers0