Storing configuration file of an application in OS independent path - java

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.

Related

Get absolute path to project directory in application.properties

I am writing a Quarkus application which reads data over http. In my application.properties file, I have this line:
my.resource=http://path/to/file
Every time I run the app, it has to download the file so I created a smaller version of the file locally for developing purpose. The problem is that I don't know how to put it in the properties file.
Ideally, I want something like this:
my.resource=http://path/to/file
%dev.my.resource=file://${project-dir}/sample_data/file
And I have to use the absolute path because I used new URI(resource).toURL() method which requires an absolute URI.
Thanks in advance.
Application properties is something that is used when your application is deployed to adopt your application to the target environment, does the user of the deployed application know anything about project directory? Project directory is something that makes sense when you are developing your application. having said that using project directory in that file does not make sense at all.

user.dir is incorrect while using tomcat web app in eclipse

I have a configuration project that few projects are using it.
All of my project under the same workingspace.
workingspace/configuration
workingspace/webapp1
workingspace/mongoDB
workingspace/model
mongoDB and the model project are using the configuration jar project and able to read the xml files using relative path ../configuration/conf/....xml
when using the
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
inside both of them, I'm getting the correct path (workspace)
while trying to do it from the web app servlet i'm getting the folder of eclipse.exe that causing some problems.
How can i fix the user.dir defalte path for the web app?
At runtime, e.g. when you're not running from inside eclipse, you probably want to work in a defined directory as well - I'd suggest to explicitly configure a specific directory. When you're running within an appserver, you might run as an unpriviledged user that doesn't have a home directory writeable at all (when the account is properly administered with minimal permissions). This differs from applications that are launched ad hoc. In fact, you probably can't assume that your application server runs as the same user as your standalone apps do.
Another alternative is to utilize the system's temp directory - if these are truly temporary files. This could be assumed writeable, or complaints if it isn't do make sense.

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.

Simulate app.config for Java?

I know that you can use java.util.Properties to read Java properties files.
See: Java equivalent to app.config?
Is there a standard place to put this file? In .NET we put application.exe.config in the same directory as application.exe. The application looks for it here by default.
Java can be made to look for a properties file in the class path but I am struggling to understand the filename/path structure to use and how to use either a standard .properties format or XML format file.
Assuming I have an API packaged in org_example_api.jar (the root package is org.example.api). I don't want to put the properties file inside the jar as it should be editable by the user. I want the user to be able to put the required configuration properties in either a .properties or .xml file somewhere relative to the classpath so I can find it without needing to know anything about the ir file system structure.
Will this work on all systems:
/classpath/org_example_api.jar
/classpath/org/example/api/config.properties OR
/classpath/org/example/api/config.xml
Code:
java.util.Properties = ? //NEED SOME HELP HERE
This purely depends on the type of application you are developing.
1) If it is a web application the best place is inside the WEB-INF/classes/ folder.
2) If you are developing a standalone application there are many approaches. From your example I think the following structure will work.
/<dist>/org_example_api.jar
/<dist>/config.xml
/<dist>/run.sh
In the run.sh you can start the java application providing the current directory also in the classpath. Something like this.
java -cp .:org_example_api.jar ClassToExecute
3) If it is an API distribution it is up to the end user. You can tell the user that they can provide the config.xml in the classpath which should follow some predefined structure. You can look at Log4J as an example in this case.
The world is wide open to you here. The only best practice is what works best for you:
Whatever program the user is running can require the path to the properties file as an argument
Your application can be configured to look in the current directory for config.properties.
If the file can't be found, you could maybe fall back to the user.home directory, or fall back to wherever your application is installed.
Personally I usually have my applications attempt to read properties files from the classpath - but I'm not in a world where I have end-users update/change the file.
Whatever option you choose, just make sure you clearly document it for your users so they know which file to edit and where it needs to be!
You can put the properties file in a directory or JAR in your CLASSPATH, and then use
InputStream is = getClass().getResourceAsStream("/path/goes/here");
Properties props = new Properties();
props.load(is);
(I noticed you mentioned this in your OP, but others may find the code useful.)

Categories