0

I am using Spring with Maven and I would like to load a file contained in the maven folder src/main/resources inside a spring service annotated with @Service contained in a JAR file (used by another WAR file as a Maven dependency). This does not seem to work for many different reasons (read: for many different exceptions according to many different attempts).

  • I know that Maven, when building, puts the stuff in src/main/resources at the root of the JAR file. I've opened the JAR file and I've verified that.
  • I would prefer a File object (something I can use with utility libraries like Apache Commons IO), but also "something" else that's working is fine.
  • I would prefere going for stuff within the Java class annotated with @Service (i.e. no application context XML file, with stuff inside that I recall within the service etc.)
  • The file contains stuff that I eventually use as a List<String> where every item is a line in the original file. I need to keep this file as it is, I can not use a database or another solution different than a file.
  • This is not a .properties file and is not intended to be.

The following are the not working attempts (throwing NullPointerException, FileNotFoundException, IllegalArgumentException: URI is not hierarchical and so on...).

Among the attempts I have tried also adding/removing the prefix src/main/resources while trying to figure out what was wrong, but then I figured out that Maven puts all the stuff at the root of the JAR file as stated before.

@Value("classpath:src/main/resources/myFile.txt")
private Resource myFileResource;

private File myFile = 
    new File(this.getClass().getResource("/myFile.txt").toURI());

private File myFile = 
    new FileSystemResource("src/main/resources/myFile.txt").getFile();

private BufferedReader bufferedReaderMyFile = 
    new BufferedReader(
        new InputStreamReader(getClass().getResourceAsStream("myFile.txt")));

What's wrong with what I am doing?

TPPZ
  • 4,447
  • 10
  • 61
  • 106
  • You say you have verified the content is in the JAR file in the root location? Then why are you using src/main/resources/myFile.txt? The root location in the JAR file would be "classpath:myFile.txt". Have you tried that? – Rob Lockwood-Blake Aug 05 '14 at 15:49
  • 1
    And did you try `InputStream inStream = this.getClass().getResourceAsStream("classpath:/myFile.txt");`, or same without `classpath:` ? – Serge Ballesta Aug 05 '14 at 15:49
  • @RobBlake I've tried all the scenarios with/without prefix, I've edited the question highlighting in bold what I stated, I think the problem was related to the `/` just before the file name, see below in this comment. @Serge your suggestion worked only in the case without the `classpath:` prefix i.e. `/myFile.txt` thanks! – TPPZ Aug 05 '14 at 16:21

1 Answers1

2

File inside jar file is no longer a File, so treat it as Resource and read it via Stream

InputStream inputStream = this.getClass().getResourceAsStream("/myFile.txt");

assumming you have actually packed that myFile.txt inside jar file and it is available at the root of JAR file

jmj
  • 237,923
  • 42
  • 401
  • 438
  • This version worked for me as well: `MyCystomSpringService.class.getClassLoader().getResourceAsStream("myFile.txt")` – TPPZ Aug 06 '14 at 08:40
  • The difference between a resource and a file (with respect to absolute paths or relative paths to the classpath of, for example, a webapp) is explained here: http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream – TPPZ Aug 06 '14 at 08:50