1

I have two project - foo and bar. Foo is the main project and it depends on bar project. Foo is also the project that holds the configuration file (conf.xml). These project are packaged as separate jars. Conf.xml is included in foo.jar and the main class accesses it like this:

MyApp.class.getClassLoader().getResource("conf.xml");

To run these i type on command line:

java -cp "*" com.example.MyApp

Everything works fine.

My question is that sometimes i want to change the conf.xml without rebuilding the project. So i would like to take conf.xml out of the jar so i can edit it myself. The problem then is that the class inside the jar cant find the conf.xml anymore. Can this be fixed without changing the code in my classes?

user1985273
  • 1,817
  • 15
  • 50
  • 85

2 Answers2

1

You have several options.

You can extract the configuration file, edit it and put it back:

# extract conf.xml from the jar
jar xf path/to.jar conf.xml

# edit the extracted conf.xml with your favorite editor
# ...

# put conf.xml back
jar uf path/to.jar conf.xml

Or instead of putting it back, you can keep in the directory without adding it back to the jar, and add the container directory to classpath before the jar:

java -cp .:path/to.jar com.example.MyApp
janos
  • 120,954
  • 29
  • 226
  • 236
0

Two pointers:

  1. The directory your conf file resides in should be on the classpath
  2. Try using MyApp.class.getClassLoader().getResource("/conf.xml");

Notice the "/" this tells java to locate it in the root of the classpath. I am assuming the file resides in the same directory from where you are running the program.

Anshu
  • 116
  • 8