0

With reference to the link: How do I read a resource file from a Java jar file?

I am trying using your code base and trying to read content of sample.csv which is residing in my project directory src/main/resources. I am unable to read the content, it says can not read file. Output:

[Can not read file: sample.csv]

//This is added within your while loop after this check /* If it is a directory, then skip it. */

I mean when file is detected then next is my below code snippet added to read the file content

if(entry.getName().contains("sample.csv")) {
   File f1 = new File("sample.csv");
   if(f1.canRead()) {
      List<String> lines = Files.readAllLines(f1.toPath());
      System.out.println("Lines in file: "+lines.size());
   } else {
      System.out.println("Can not read file: "+entry.getName());
  }
}

Can anyone educate me what I am doing wrong here, how can I make it working?

My requirement is this:

  1. (My micro-service) Service.jar imports Parser.jar library in its pom.xml
  2. (My library) - Parser.jar has FnmaUtils-3.2-fieldMapping.csv file in src/main/resources directory
  3. There is a FnmaUtils class that loads the FnmaUtils-3.2-fieldMapping.csv within its constructor, this class is part of Parser.jar - Here I am trying to read the content FnmaUtils-3.2-fieldMapping.csv, this step is keep failing with below error, tried all possible options shown in [How do I read a resource file from a Java jar file?

    public FnmaUtils() {
       String mappingFileUrl = null;
       try {
           Resource resource = new ClassPathResource("FnmaUtils-3.2-fieldMapping.csv");
           mappingFileUrl = resource.getFile().getPath();
           loadFnmaTemplate(mappingFileUrl);
       } catch (Exception e) {
           e.printStackTrace();
           LOGGER.error("Error loading fnma template file ", e);
       }
    }
    

Getting error:

java.io.FileNotFoundException: class path resource [`FnmaUtils-3.2-fieldMapping.csv`] cannot be resolved to absolute file path because it does not reside in the file system: `jar:file:/home/ravibeli/.m2/repository/com/xxx/mismo/util/fnma-parser32/2018.1.0.0-SNAPSHOT/fnma-parser32-2018.1.0.0-SNAPSHOT.jar!/FnmaUtils-3.2-fieldMapping.csv`
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:218)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at com.xxx.fnma.util.FannieMaeUtils.<init>(FannieMaeUtils.java:41)
at com.xxx.fnma.processor.FNMA32Processor.<init>(FNMA32Processor.java:54)
at com.xxx.fnma.processor.FNMA32Processor.<clinit>(FNMA32Processor.java:43)

What is going wrong here?

halfer
  • 19,824
  • 17
  • 99
  • 186
ravibeli
  • 484
  • 9
  • 30

1 Answers1

0

Try

InputStream in = this.getClass().getClassLoader() .getResourceAsStream("SomeTextFile.txt");

Be sure the resource is in your classpath.

Alexander Petrov
  • 9,204
  • 31
  • 70
  • Yes Alex I tried that too. Still see the same issue – ravibeli Mar 02 '18 at 13:43
  • @ravibeli do you have classpath reference in the manifest file from Service.jar file to Parser.jar file ? – Alexander Petrov Mar 02 '18 at 13:45
  • Can you open the jar file with 7zip or something manualy and verify the csv file is inside ? – Alexander Petrov Mar 02 '18 at 13:46
  • Yes csv file exists in the Parser.jar, I extracted and verified in 7zip. Also verified the MANIFEST.MF file has an entry for the FnmaUtils-3.2-fieldMapping.csv as well, since I have added manifest entries in pom with FnmaUtils-3.2-fieldMapping.csv. – ravibeli Mar 02 '18 at 18:25