Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java OutputStream equivalent to getClass().getClassLoader().getResourceAsStream()

I am attempting to store the change made to my application's properties. The .properties file is located in resources package, which is different from the package that contains my UI and model.

I opened the package using:

this.getClass().getClassLoader().getResourceAsStream("resources/settings.properties")

Is there a functional equivalent of this that permits me to persist changes to the Properties Class in the same .Properties file?

like image 909
craig Avatar asked Jan 08 '10 22:01

craig


2 Answers

In general, you cannot put stuff back into a resource you got from the classloader:

  • Class loader resources are often read-only; i.e. held in read-only files / read-only directories.

  • If you got the resource from a JAR file, JAR files are not simply updateable. (To "update" you need to extract the old JAR's contents and create a new JAR with the updated contents. It is all to do with the structure of ZIP files ...)

  • In some cases, the class loader resource will have been downloaded on-the-fly, and there is no way to push changes back to the place where you downloaded from.

Even if you can update a resource you got from the classloader, it is a bad idea / bad practice.

  • Doing this "pollutes" the clean application installation with a user's preferences. Among other things, this means that the installation cannot be shared with other users (unless you handle preferences for multiple users ...).

  • There are security issues with having applications installed as writeable so that embedded preferences can be updated. Think viruses! Think one user who might be inclined to trash another user's preferences!

  • There are administration issues with having user-specific copies of applications. And if the user has to install his own copy of an app, there are potential security issues with that as well.

  • There may be technical issues with file locking or caching on some platforms that either get in the way of (safe) updates or make it difficult for an application to load the updated resource without a restart.

Finally, this is NOT the way that system administrators (and educated users) expect software to behave. Java applications should deal with user preferences in the expected way:

  • You can use the Java Preferences API.

  • You can write a Properties file containing the preferences to an OS-appropriate user-writable directory.

  • On Windows, you could use a Windows-specific API to store the preferences in the Windows registry, except that this makes your application Windows dependent. (I can't see any real advantage in doing this, but I am not a Window expert.)

like image 155
Stephen C Avatar answered Oct 23 '22 15:10

Stephen C


When you wrap your app up as a JAR file, your properties file will be one (possibly compressed) file within that JAR, and it would be a bad idea to try to write to your own JAR.

getResourceAsStream() is meant to open resources for reading, and these can be anywhere on the classpath. You can't write to URLs or inside JARs, you can only write to files, so it doesn't make sense to give you the same API for output.

Find yourself a directory you're allowed to write into, and write your properties there.

It may be a good idea to copy your properties from your installation classpath (possibly inside a JAR) directly out to a file if it doesn't yet exist, as a first operation upon application startup. This will give you a properties file you can write to, yet the master copy of this properties file will come from your project deliverable.

like image 45
Carl Smotricz Avatar answered Oct 23 '22 16:10

Carl Smotricz