Owen
Owen

Reputation: 22887

How do I access a config file inside the jar?

I'm using FlatPack to parse and load data from flat files. This requires loading a config file that stores mappings of the columns of the flat file.

I have a constant to define the location of the mapping file:

private static final String MAPPING_FILE = "src/com/company/config/Maping.pzmap.xml";

I have a parse(File dataFile) method that actually does the parsing:

private void parse(File dataFile) throws FileNotFoundException, SQLException {
        Parser parser;

        log.info("Parsing " + dataFile.getName());

        FileReader mappingFileReader = new FileReader(MAPPING_FILE);
        FileReader dataFileReader = new FileReader(dataFile);

        parser = DefaultParserFactory.getInstance().newFixedLengthParser(mappingFileReader, dataFileReader);
        parser.setHandlingShortLines(true);

        DataSet dataSet = parser.parse();

        //process the data
}

When I jar up everything and run it as a jar - it bombs out on FileReader mappingFileReader = new FileReader(MAPPING_FILE); with a FileNotFoundException. That file is inside the jar though.

How do I get to it?

I've looked at this question and this question about accessing files inside jars and they both recommend temporarily extracting the file. I don't want to do that though.

Upvotes: 7

Views: 41046

Answers (5)

Shubham Jain
Shubham Jain

Reputation: 17553

Use Below code in your config file

InputStream in = Class.getClassLoader().getResourceAsStream(configFilePath);

Note : replace the classname with your Config class

Demo Full code :

Imports:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

Code:

    Properties properties = new Properties();
    InputStream in = MyCLassName.class.getClassLoader().getResourceAsStream(configFilePath); //pass the config.properties file path with file name
    try {
        properties.load(in);
        properties.getProperty("username"); //here put the key from config.properties
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Upvotes: 0

user115086
user115086

Reputation: 139

Use Apache Commons Configuration, then you can read/write XML, auto update, find config file in path or jar, without a lot of hassles.

Upvotes: 0

Billy
Billy

Reputation: 952

If I remember correctly, getResourceAsStream() can behave differently depending on which web server your webapp is deployed, for instance I think it can be a problem when deployed as a war on a Websphere instance. But I'm not sure if this applies to you.

But I'm not sure you're trying to solve the "proper" problem : if it's a config file, that means is data dependant right ? Not code dependant ( your jar ) ? When the flat file will change, your config file will need to change as well, right ? If this is true, it sounds like the config should be better stored elsewhere, or even passed as a parameter to your jar.

But maybe I haven't fully understood your problem...

Upvotes: 1

skaffman
skaffman

Reputation: 403461

if it's inside a JAR, it's not a File, generally speaking. You should load the data using Class.getResourceAsStream(String), or something similar.

Upvotes: 15

Related Questions