简体   繁体   中英

Where should a java application create files when it is run from Eclipse?

When my Java application is run with "Run application" from Eclipse it needs to create files that persist across multiple runs of the application. Is it an acceptable practice to place these in the "target" directory? For example, workspace//target/database.db.

Are there events that cause eclipse to delete all files in the target directory?

If the target directory is not a good place to store these files where is a good place? It would be awkward to use System.getProperty("java.io.tmpdir") since the tmpdir is on a different drive and because I would need to mirror the structure of the workspace and project in the tmpdir.

Thank you

The target directory gets deleted when you clean your project ( mvn clean ). That makes this not the best place to store your data. You should better save it to System.getProperty("user.home") , or pass a directory as a program argument as SpaceTrucker mentioned in the comment, that`s also a good solution.

My recommendation would be the OSGi User Area ( -user , intended for user-specific preferences etc.), or the OSGi Instance Area ( -data , which is the default home of The Workspace). Take a look at this , and scroll down to "Locations", which explains a bit about these areas.

As for hazardous events, only -clean (the Eclipse arg, not the Maven target) and your deployment process come to mind. -clean should not touch persisted files but works on the internal caches, only, but you might want to try that out to be sure. Your deployment process might consist of "delete contents of installation directory" plus "copy new stuff to installation directory", which would wipe most of the areas which are by default placed inside the installation directory.

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