where to put application data and temp files on mac - java

We have a Windows Java desktop application that creates some configuration files at %APPDATA% and creates logs at %TEMP% location. Now we are planning to release that desktop application for Mac OS X. We are facing following difficulties:
We do not want to keep application data within .app (file extension app) so please suggest where else we can keep our configuration files.
We do not want to keep temporary files at %TEMP% location in Mac OS X because Mac OS X automatically cleans the %TEMP% location as per schedule.
So we do not want data lose from temp and do not want to keep data with in .app. Please suggest.
Thanks

You should probably put the files under /Library/Application Support/<your app> or ~/Library/Application Support/<your app>
If the resources apply to all users
on the system, such as document
templates, place them in
/Library/Application Support. If the
resources are user-specific, such as
workspace configuration files, place
them in the current user’s
~/Library/Application Support
directory.
Source: Determining Where to Store Your App-Specific Files

We do not want to keep application data within .app (file extension app) so please suggest where else we can keep our configuration files.
Use Preference API
We do not want to keep temporary files at %TEMP% location in MAC OS because MAC OS automatically clean %TEMP% location as per schedule.
Create a dir under user.home to keep temp data

Either ~/Library/Application Support or ~/Library/Caches come to mind. Use the APIs if possible.

Related

How to tell if a directory on a Linux machine has an external file system mounted to it in Java?

I have three separate Linux servers that mount and share the same single file system under a directory called /efs
I have a Java application that uses this file system, and needs to be able to verify that the file system has been mounted correctly (Or else, it would simply write to /efs on the local machine instead of the shared storage without knowing) - How would I detect at run time from my application that the file system has been mounted to the directory?
Sorry if this is a duplicate question. I really did try to find information on this but I couldn't find a clear answer.
I see a few approaches to this problem:
If the mounted filesystem at /efs is different from the root filesystem, you can compare them using Files.getFileStore(path).type(). The filesystem for / being the same would then be a clear indicator that the /efs mount is missing. This assumes JDK >= 7.
Read and parse /proc/mounts to see which file systems are mounted and with which options. This is the same data source Java's FileStore API uses under the hood, so you might take the parsing logic directly from the JDK sources. This would be independent on the Java version.
Have a file on the mounted filesystem which is never on the root filesystem which can then be checked for simply by Files.exists(path) or new File(path).exists(). This would be independent from the Java version and even independent from Linux.
Answering my own question - It just occurred to me to create a file in the mounted file system and simply check the file exists avoiding any OS dependent code.

How to refer a file system in Cloudbess?

I'm new to Cloudbees. I just opened an account and trying to upload a .JAR file which basically downloads a file to the location mentioned by user (through java command line arguments). So far I have run the .JAR in my local. So far, I was referring to my local file system to save the file. If I deploy my .JAR file to Cloudbees SDK, where can I save the downloaded file (and then process it).
NOTE: I know this is not a new requirement in java if we deploy the jar in UNIX/WINDOWS OS where we can refer the file system w.r.t to home directory.
Edit#1:
I've seen couple of discussions about the same topic.
link#1
link#2
Everywhere they are talking about the ephemeral (temporary) file system which we can access through System.getProperty("java.io.tempDir"). But I'm getting null when I access java.io.tempDir. Did anyone manage to save files temporarily using this tempDir?
You can upload a jar with the java stack specifying the class and classpath (http://developer.cloudbees.com/bin/view/RUN/Java+Container)
Our filesystem however is not persistent, so if you are talking about saving a file from within your application, you could save it in this path
System.getProperty("java.io.tmpdir")
but it will be gone when your application hibernates, scales-up/down or is redeployed.
If you want a persistent way to store file/images, you can use AmazonS3 from your cloudbees application: uploading your files there will ensure their persistence.
You can find an example of how to do that in this clickstart:
http://developer-blog.cloudbees.com/search?q=amazon+s3
More information here: https://wiki.cloudbees.com/bin/view/RUN/File+system+access

Storing configuration file of an application in OS independent path

I am writing an application (basically eclipse plugin), so there are few combo-box, drop-downs etc, which I am getting values for them dynamically via XML file. My doubt is which is the best way to store these files in a particular directory so that it can be read in both Windows as well as Linux.
Initially I tried to create a config path under {eclipse.home.location} like:
String finalPath = System.getProperty("eclipse.home.location") +"/myAppConfig";
and store all of my plugin's configuration there (not only configuration but few helper jars which I programatically read in my plugin). But there is a probabilty that Eclipse installation maybe in shared location and user may not have write access to it.
I also tried to store it in a program files directory using:
System.getenv("ProgramFiles");
But this fails under non-windows environments. So my question is can anyone shed a light on this so that I can store in some common directory where it is valid for both windows and linux?
Kindly let me know if my wordings are confusing. Or is it possible to store my config files under plugins directory and get the path like this /plugins/myConfigDir ?
Try using the getStateLocation() method in Plugin.
That will give you an IPath that points to a user specific workspace location.

Java on MacOSX: how to access files in the bundle

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?
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.
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.

Java: Where to write config without requiring administrative rights

I actually can't believe I'm saying this but since porting my programs to OSX and getting used to permissions, I've realized that what I planned to do on Windows will not work how I want it to. Currently, on windows, my program stores it's setting's in the registry (HKLM) and some user editable resources in a folder next to the program file. For various reasons, I have now decided that the configuration/settings will be stored in a file and the user will be able to in which folder the other resources are kept.
So the question I have now is where to store the configuration file. Obviously it will be updated, but I don't want to program to have to require administrator permissions to run. I would like to offer an option so that all users can use the program (like most programs do), which will of course require Admin, so this leads be onto the second query: where should I store the configuration file (and the folder in which other resources are kept) and how can I detect whether the program has been installed for all users or just one!
Thanks in advance
PS If you didn't guess, the program is written in Java so I would like to know how to programatically get the location you suggest as well please.
Its normal practice in *nix compatible programs to store information in folders starting with name . in the home directory of the users like,
.bash_history
.bashrc
You could use the same on OSX in my opinion and create a directory say,
.myapp
You can store any number of files with any format under that directory.
To get the location of the folder, you can do
String homeDir = System.getProperty("user.home");
File myAppDir = new File(homeDir, ".myapp");
That is roughly the code that can get you your custom config directory for your app.
Please not that dot files / folders are somewhat similar to hidden folders in windows. Your File Manager will not generally show these files / folders by default.
To identify if the program is installed for all the users or not, you could create the configuration at some administrator (root) controlled location like /etc (not sure about Mac) The user configuration can always override the default config. There could be a better way to handle this though.
On both windows and unix, User(usually) has a (home)folder to which it has full permissions. You may create a directory in the home folder and have your user configuration files reside there.

Categories