Search code examples
javatomcatioapplication-server

Delete file from deployed web app from within the app


I have a Java web application that I build as WAR and deploy on Apache Tomcat. Let's say that I have a mechanism built-in my app to determine when it is successfully deployed and running on the server (I'll call it SuccessHandler). Conditionally in the SuccessHandler I need to programmatically delete a file from the project's deployment directory (say path-to-tomcat/webapps/my-project/file.txt) how can I do that?

Disclamer: I am able to load/delete files from the resources directories that reside in path-to-tomcat/webapps/my-project/WEB-INF/ after the deployment + I am able to delete the files using hard-coded absolute path but this is not what I am looking for. I want to be able to delete files no matter where the application is deployed.


Solution

  • You can't do it reliably. The inside of the application isn't writable in the general case. In particular, the archive may be executed in place and not exploded; even if it isn't, there's nothing keeping the server from restoring the file at every launch. You'll need another way to save your state.