Classpath resource not found when running as jar

resource.getFile() expects the resource itself to be available on the file system, i.e. it can’t be nested inside a jar file. This is why it works when you run your application in STS (Spring Tool Suite) but doesn’t work once you’ve built your application and run it from the executable jar. Rather than using getFile() to access the resource’s contents, I’d recommend using getInputStream() instead. That’ll allow you to read the resource’s content regardless of where it’s located.

Leave a Comment