1

I have written this project which has a bunch of config files in config/ folder, which are necessary for running: https://github.com/danyaljj/jwnl-prime/

The code is working fine under mvn test in my computer and CI.

But after packaging it and adding it as a maven dependency to another project, the function calls give me error

java.io.FileNotFoundException: config/file_properties.xml (No such file or directory)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:195)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at java.io.FileInputStream.<init>(FileInputStream.java:93)
        at net.didion.jwnl.TestDefaults.getInputStream(TestDefaults.java:63)
        at net.didion.jwnl.JWNL.initialize(JWNL.java:92)

Any idea why I am getting this weird behavior?

Update: after extracting the jar file, it turns out that the config files are not included in the jar: enter image description here

Update2: After adding the suggestion by ? the config files get included in the jar file: enter image description here

But still getting the same error: config/file_properties.xml (No such file or directory)

Daniel
  • 5,839
  • 9
  • 46
  • 85

2 Answers2

3

If you have project structure other than standard format you need to update pom.xml as well. If you need to include the config for build you need to add that in build section like

Refer link enter link description here

<build>
    <extensions>
      <extension>
  <groupId>org.apache.maven.wagon</groupId>
  <artifactId>wagon-ssh</artifactId>
  <version>2.4</version>
   </extension>
    </extensions>
    <resources>
        <resource>
             <directory>config</directory>
             <includes>
                 <include>**/**</include>
             </includes>
        </resource>
    </resources>
</build>
vinothM
  • 195
  • 1
  • 6
  • Good point. I did it and the config files got attached to my jar file; but still has problem in reading (See the updates I added to the question). – Daniel Nov 06 '15 at 01:29
  • You should also make sure if the path of the file is correct, i am not able to build your project some how so not able to see how the final jar look like. But if the config folder is in same level of net/ "the root package" then you need to refer like "/config/file_properties.xml" otherwise the path is relative to the java class which refers it. the forward slash make it absolute path – vinothM Nov 06 '15 at 01:38
  • I have actually included an screenshot of the extracted jar file in the question (see the updates). – Daniel Nov 06 '15 at 01:40
  • Check the path accordingly then – vinothM Nov 06 '15 at 01:41
1

First, verify that the configuration files are actually being packaged into the jar. I would view it in an archive viewer or unpack it and see what's in it.

Secondly, you can't directly load a file from a jar using standard file opening methods. You'll have to use something like

Class.getResourceAsStream() 

See here for some more suggestions: How do I access a config file inside the jar?

Community
  • 1
  • 1
whaleberg
  • 2,093
  • 22
  • 26