0

I'm trying to write a converter for specific file formats. The converted files should be filtered using a configuration file. Everything works great so far, but I have a problem concerning the reading of a file in a packaged jar.

I have the following structure for my files (Windows):

converter/src/main/resources/files/AStoXES.properties (there are 3 more property files)

If I'm executing this code in Netbeans everything works fine and the file will be read:

this.fileConfig = new PropertiesConfiguration(new File(testing.class.getClassLoader().getResource("files/AStoXES.properties").toURI()));

But if I'm packaging the jar with Apache Maven doing a clean install and run the whole program I'll get a java.lang.IllegalArgumentException: URI is not hierarchical.

After hours of spending time with Google I have found many threads with the same problem and tested lots of things but none of those proposals helped me out of this problem.

I NEED the contents of the file as a java.io.File, so using Inputstream with getResourceAsStream(argument) won't help me out, because I need to work with the properties in there.

Someone knows how to figure this one out?

X-Fate
  • 323
  • 4
  • 13
  • possible duplicate of [why my URI is not hierarchical?](http://stackoverflow.com/questions/18055189/why-my-uri-is-not-hierarchical) – André Stannek Jan 13 '15 at 10:26
  • @André Stannek: That's not a duplicate!! I CANNOT use `getResourceAsStream`! – X-Fate Jan 13 '15 at 10:29
  • 1
    I might be wrong but to my knowledge you have no way around `getResourceAsStream()`. See also http://stackoverflow.com/questions/333363/loading-a-properties-file-from-java-package – André Stannek Jan 13 '15 at 10:29
  • You could try to programmatically unzip your own jar file into a temporary folder and access the unzipped files there. – slartidan Jan 13 '15 at 10:32
  • possible duplicate of [How to get a path to a resource in a Java JAR file](http://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file) – Joe Jan 13 '15 at 10:52
  • @AndréStannek: The link was very useful. Thanks. And Joe: Read my question carefully - that's not a duplicate at all. – X-Fate Jan 13 '15 at 11:15

3 Answers3

1

Solved it by following the link (Loading a properties file from Java package) AndréStannek provided.

This way it could be read. Didn't know that PropertiesConfiguration accepts InputStreams as well.

            InputStream in = testing.class.getClassLoader().getResourceAsStream("files/AStoCSV.properties");
            this.fileConfig = new PropertiesConfiguration();
            this.fileConfig.load(in);
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(ConverterControl.class.getName()).log(Level.SEVERE, null, ex);
            }

Thanks to all.

Community
  • 1
  • 1
X-Fate
  • 323
  • 4
  • 13
0

Try to get the runnig jar file, and access it as a zip file.

This may help:

How to get the path of a running JAR file?

Community
  • 1
  • 1
0

As a solution/hack, you may create a temporary File (and an OutputStream corresponding to this File) then "write" your input stream into your output stream. You'll end up with a copy of your original File.

ferjani
  • 89
  • 8