2

I am running a tool (a virtual globe) through code from a jar file. The code reads a resource (an XML file) to provide some configuration options, using syntax like this, in a method of class Config:

URL localURL = Config.class.getResource("Config.xml");

I would like to provide my own Config.xml file with settings that override those in the file contained in the jar file.

I am not clear about how I can do this. I understand that the getResource() method explores the classpath to find the resource. So I thought of this: - putting a copy of the file with my own settings in a specific directory - putting this directory in front of the classpath

But to no avail: the getResource() still always loads the resource from the jar file.

I must be missing something ...

I tried removing the Config.xml file from the application jar. That fails: the application fails because getResource() returns null. It seems to me like

Config.class.getResource("Config.xml") 

only looks for resources inside the jar file that contains class Config, whereas I thought it was looking in the classpath.

Albert Godfrind
  • 1,978
  • 1
  • 12
  • 20
  • The quickest thing to do would be to open the jar (with winrar, for example) and replace the Config.xml file. – NotGaeL Jul 04 '14 at 18:49
  • Can't you add your modified file into the jar directly? – user1071777 Jul 04 '14 at 18:49
  • Sure, I could do that. But I really wanted to avoid it. And I really would like to allow the users to customize their execution environment by providing their own custom configuration settings. – Albert Godfrind Jul 04 '14 at 18:53
  • check if a config file exists in the place you want it before using getResource() and load this instead – kaetzacoatl Jul 04 '14 at 19:01
  • You can only do this, by placing your own Config.xml "earlier" in the classpath of the application. – Thorbjørn Ravn Andersen Jul 04 '14 at 19:24
  • That is what I have been trying to do. My classpath is like this: ./config:....:./jar/appl.jar where "appl.jar" is my application jar and ./config is the directory with my own Config.xml file. But I still always get the file from the jar. – Albert Godfrind Jul 04 '14 at 19:49
  • Got it. The trick is that getResource() looks for a fully qualified name. Since my class is really "vis.globe.Config:, it looks for vis.globe.Config.xml. Therefore I need to place the file in ../config/vis/globe/Config.xml and place ../config at front of the classpath. – Albert Godfrind Jul 05 '14 at 12:27

2 Answers2

4

OK, got it. The issue is this: my Config class is really in a package, i.e. vis.globe.Config.java, so getResource("Config.Xxml") really looks for a file called vis.globe.Config.xml.

Therefore, with a classpath such as "../config:../jar/appl.jar", it will look for file Config.xml, not in ../config, but in ../config/vis/globe.

So the solution was to create a sub-directory structure in ../config that reflects the fully qualified name of class Config.java, i.e.

../config/vis/globe/Config.xml
Albert Godfrind
  • 1,978
  • 1
  • 12
  • 20
0

You could always look to do something programmatically. So for example you would first search for a file named UserConfig.xml and if it was not found fall back on the file named Config.xml which would be found in the jar file.

dbarton_uk
  • 980
  • 11
  • 17