Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access a config file inside the jar?

Tags:

java

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.

like image 540
Owen Avatar asked Jun 03 '09 13:06

Owen


People also ask

How do I view inside a JAR file?

JAR files are packaged in the ZIP file format. The unzip command is a commonly used utility for working with ZIP files from the Linux command-line. Thanks to the unzip command, we can view the content of a JAR file without the JDK.

How do I open a config file?

To open a CFG file using the native Notepad app, open Windows File Explorer at the location of the file. If Windows automatically recognizes the CFG file, double-click it to open it in Notepad. Alternatively, right-click the CFG file and select the Open With option.

Where can I find config file?

Most global config files are located in the /etc directory. The /etc/ directory feels more like a filesystem and has many sub-directories, each having related config files. The following is a list of the most useful of these sub-directories: /etc/X11/ – xorg specific config files.


2 Answers

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.

like image 64
skaffman Avatar answered Oct 06 '22 20:10

skaffman


I think it is solved just here:

http://www.velocityreviews.com/forums/t129474-beginner-question-how-to-access-an-xml-file-inside-a-jar-without-extracting-it.html

like image 22
ahmet alp balkan Avatar answered Oct 06 '22 19:10

ahmet alp balkan