0

I have created an application which does some job and it has a configuration folder inside it(named /xml/Configuration.xml).Now, I am trying to use the same jar in multiple projects and the only change which I want to make it is by changing the configuration file contained within the jar file.

String xmlConfigPath = System.getProperty(user.dir) + File.separator
                + "xml" + File.separator + "Configuration.xml";

As you can see here, I access the XML file by using the "user.dir" followed by the complete file name.This(user directory) obviously will change when the jar added in a project is run from a different user.dir which hence does not find the Configuration.xml file placed inside the jar file added to my project.

Is there a way to make this jar access its own configuration folder irrespective of which project or location its placed in?

Please see the following code Inside my jar file I have the following function which loads the configuration

/**
     * This method is responsible for reading the EMail Configuration
     * @return <MailSender>
     * @throws Exception 
     */
    private static MailSender getConfig(String configPackagePath, String fileName) throws Exception  {
        JAXBContext jaxbContext;
        Unmarshaller unmarshaller;
        StreamSource source;
        JAXBElement<MailSender> mailSenderElement;
        MailSender mailSenderConfig = null;
        String xmlConfigPath = getUserDirectoryPath() + File.separator
                + ApplicationConstants.XML + File.separator + fileName;
        try {
            jaxbContext = JAXBContext.newInstance(configPackagePath);
            File file = new File(xmlConfigPath);
            unmarshaller = jaxbContext.createUnmarshaller();
            source = new StreamSource(file);
            mailSenderElement = unmarshaller.unmarshal(source, MailSender.class);
            mailSenderConfig = mailSenderElement.getValue();
        } catch (JAXBException e) {
            String errorMessage = "Error encountered while reading the EMail XML Configuration file at " + xmlConfigPath;
            throw new Exception(errorMessage, e);
        }
        return mailSenderConfig;
    }

    /**
     * This method is responsible for fetching the User Directory Path
     * @return <String> userDirectoryPath
     */
    private static String getUserDirectoryPath() {
        return System.getProperty(ApplicationConstants.USER_DIR);
    }

Now, while running this as a standalone project, it finds the correct path and works fine.Now, when I package this as a jar, this fails because it cannot fetch the file from the current user directory.The intention here is I want the jar file to use absolute path to access the file already placed inside it without having anything to do with the location of where it(jar file) might itself be placed.

Anish
  • 255
  • 2
  • 4
  • 15

3 Answers3

0

You should instead put the configuration file on the classpath of your application and load it using ClassLoader.getResourceAsStream

This means for example using xml/ as source folder inside Eclipse, then you can get the configuration file from anywhere using :

getClass().getResourceAsStream("/xml/Configuration.xml");
Antoine Marques
  • 1,379
  • 1
  • 8
  • 16
0

Some suggestions:

Community
  • 1
  • 1
qwazer
  • 7,174
  • 7
  • 44
  • 69
0

If the file is within the JAR file then you should use something like:

File conf=new File(getClass().getResource("/xml/Configuration.xml").toURI());
// The "absolute path" here points at the root of the JAR
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59