positioning files for use from both IDEs and JARs

Thomas L Roche <tlroche-r/[email protected]>
Newsgroups gmane.org.user-groups.trijug.juglist
Message-ID <OF40AC919C.80BEED99-ON85257292.00800699-85257292.00824B8C@us.ibm.com>
I'm wondering how to position a file (either a .properties or a
preferences XML) in filespace so as to satisfy all the following
requirements: it can be

* programmatically read from classes in an Eclipse project, e.g. when
  launching a Java application

* programmatically read from classes in a deployed JAR file

* when deployed, read and written by a human outside the JAR. I.e. a
  user could run the app, edit the .properties, re-run the app, etc,
  without needing to open or rebuild the JAR.

Details:

I'm developing a plain-old command-line app in Eclipse. I want classes
in the code to be able to read from a file when I launch the code, so
I'm assuming I want the file to be in the project's filetree: assume
that's rooted @

laptop:/path/to/eclipse/myworkspace/myproject

However I also want to be able to deploy the code in a JAR remotely,
i.e. I should be able to do something like

[laptop $] scp 
/path/to/eclipse/myworkspace/myproject/{prefs.xml,build/my.jar} 
remote:/path/to/deploy/
[remote $] java -jar /path/to/deploy/my.jar

I want users to be able to edit the file without having to hack the
JAR. I.e. I want to support a usecase like

[remote $] java -jar /path/to/deploy/my.jar
[remote $] edit /path/to/deploy/prefs.xml
[remote $] java -jar /path/to/deploy/my.jar

I don't want to hafta specify the path to which the JAR must be
deployed, but could do that if absolutely necessary. I'd also prefer
to specify a JAR-relative path to the file: e.g. the user should be
able to do something like

Finally I want to be able to use the same API to load the file in both
cases (whether running from eclipse or from deployed JAR).

In neither case do I have control over where the user's JVM is
located: probably the only things I can dictate are

* the path to the file inside the Eclipse project

* where the JAR is deployed

* where the file is deployed. I'd prefer to specify that it be located
  relative to the JAR, but could also give an absolute path (e.g.
  /tmp/foo/bar.properties)

I'm wondering, can I satisfy all the requirements? If so,

0 Where to locate the .properties file for both the eclipse project
  and the deployed JAR?

1 Which API to use? I'm aware of several (besides the File* ones),
  e.g.

ClassLoader.getResourceAsStream
ClassLoader.getSystemResourceAsStream
Class.getResourceAsStream
ResourceBundle.getBundle

2 Does the file just need to be on the classpath? If not, how to
  specify the path to the file (or the file's URL) in the API call?

If this can't be done, is there a next best alternative? (Short of
passing all the properties on the commandline ?-)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.