0

As discussed https://stackoverflow.com/a/22632251/379235, I added my code as

  @Value("classpath:lpLogCompressFileLocations")
  private Resource logLocations;

and use it as

  Compressor getCompressor() throws IOException {
    System.out.println("logLocationsFileExists:" + logLocations.getFile().exists());
    return new Compressor(".", logLocations.getFile(), ZIP_NAME_PREFIX);
  }

in my jar file I can locate this file as well

$ jar -tvf target/original-Processor.jar  | grep lpLog
    60 Wed Apr 15 12:19:02 PDT 2015 lpLogCompressFileLocations

But when I deploy this code and try to access, I get

15 Apr 2015 12:17:56,530 [DEBUG] [http-bio-8443-exec-3] ReportSender             | Error in starting compression: class path resource [lpLogCompressFileLocations] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/shn/lp/original-LogProcessor.jar!/lpLogCompressFileLocations

I also tried changing my code to

@Value("classpath:/lpLogCompressFileLocations")
private Resource logLocations;

But the error is same

15 Apr 2015 12:19:30,984 [DEBUG] [http-bio-8443-exec-1] ReportSender             | Error in starting compression: class path resource [lpLogCompressFileLocations] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/shn/lp/original-LogProcessor.jar!/lpLogCompressFileLocations

What am I missing?

Community
  • 1
  • 1
daydreamer
  • 87,243
  • 191
  • 450
  • 722

1 Answers1

0

You cannot get a file handler to resource existing within a jar file. It is explained here. Java Jar file: use resource errors: URI is not hierarchical

I am not sure if this solves your purpose, but you can get access to the content as below.

@Value("classpath:lpLogCompressFileLocations")
  private Resource logLocations;

Within your method(sample code)

BufferedReader reader = new BufferedReader(new InputStreamReader(this.logLocations.getInputStream()));
        StringBuilder out = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
        System.out.println(out.toString()); // Prints the string content read from input stream
        reader.close();
Community
  • 1
  • 1
minion
  • 4,313
  • 2
  • 20
  • 35