0

I have a Java app which I am packaging to a Mac Application Bundle (That folder structure that contains all of the app but looks like a single executable file to the user).

My Problem: I am reading and writing some config files in the local folder ("."). However, on Mac this seems to be the folder in which the application bundle is located (so usually the "Applications" folder and I obviously don't want that.

My question: How can I store a file inside that bundle? How can I programmatically retrieve the bundle name to compute the fully qualified folder?

I know I could try to go the ClassLoader way, but I'd like to avoid that (for security reasons).

Or is there simply a better way how to store application cache and config data locally?

Daniel
  • 2,087
  • 3
  • 23
  • 37
  • Please read the link. If you don't get a good answer then you should answer it yourself. You should also reward good work even if it doesn't answer your question fully. – Gray May 19 '12 at 22:11
  • I accept an answer if it solves the problem (or is a good workaround). Good work is rewarded by voting up. If none of the answers help me with the problem then none is accepted. And if I didn't answer it myself then because I haven't solved the problem yet. – Daniel May 19 '12 at 22:19

2 Answers2

1

The Mac OS X Finder treats any directory whose name ends in .app as an application; right-click to Show Package Contents. It remains an otherwise normal directory for I/O purposes. This project is an example. See this answer regarding paths relative to the application bundle.

Addendum: Is there a better way how to store application cache and config data locally?

The example cited uses java.util.prefs.Preferences, but javax.jnlp.PersistenceService is an alternative.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • sorry, it's a good answer, just not really for my question. The question was how can I obtain the name of the folder (i.e. what is the name of the .app folder?) Using preferences is not suitable for cache data (can only handle small data volume) – Daniel Aug 28 '12 at 21:14
  • On Mac OS X, `java.util.Preferences` is implemented via an XML file in `~/Library/Preferences`; my largest is about a megabyte. I don't mind a `~/.appname` folder; `~/.netbeans` is an example. @You's suggestion is a recommended alternative. – trashgod Aug 28 '12 at 23:39
1

Ok, the basic answer / solution is: don't do it.

The reason I originally wanted to do it was to cache larger amounts of data on the local HD. Java preferences are a good choice for config data (i.e. small data amounts) but fail to handle data in the megabyte size range.

My solution:

On MacOSX (System.getProperty("os.name").contains("Mac OS X")) I simply create a folder in the user's home folder (System.getProperty("user.home")). I prefix that folder with a . to ensure it is hidden from the user. This also ensures that I have write access to the folder (which could be a problem in the .app folder depending on where the user copies it)

On Windows (System.getProperty("os.name").contains("Windows")) I create that folder in the System.getenv("APPDATA") directory (note that this env variable only exists on Windows systems.

Now I have full access to the filesystem (even without admin rights) and can store as much data as I like.

Daniel
  • 2,087
  • 3
  • 23
  • 37
  • 2
    Actually, cache data should be in something like `(~)/Library/Caches/` or (if more permanent, like large databases and stuff) `(~)/Library/Application Support/`. I know I don't want dotfiles with large caches in my home directory, and I'm probably not alone. – You Aug 28 '12 at 21:34
  • good point, I think I will use this instead (assuming I do not need root rights to write there). Thanks! – Daniel Aug 29 '12 at 19:29