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.
Spring-Boot Resource Not Found when using executeable Jar
Okay, already I found the real problem and the solution.
First, the application use the correct path to the csv files, but there is another issue when using an executable jar what I found under following link. Stackoverflow-Link
Before I come to issue with executable jar I used following solution for getting CSV-File (Issue is getFile()):
final List<String> resourceLines = FileReadUtils.readLines(specialisationResource.getFile());
for (final String line : resourceLines) {
data.add(getNewTransientSpecialisation(line));
}
But in executeable jar I cant use my resource as file, I need to use it as stream, see provided link above. So I needed to change my code. If you prefer using native java, you can do follows:
final InputStream inputStream = specialisationResource.getInputStream();
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null) {
data.add(getNewTransientSpecialisation(line));
}
I prefer using frameworks and use apache commons like follows:
final List<String> resourceLines = IOUtils.readLines(specialisationResource.getInputStream());
for (final String line : resourceLines) {
data.add(getNewTransientSpecialisation(line));
}
So just remember, don't use File() for getting resource, always use stream do avoid that issue from beginning :-)
Hope that helps someone.
Can't locate resource file in JAR
You need to read particular file from that directory as a classpath resource instead of java.io.File
getClass().getResourceAsStream("relative/class/path/to/resource");
Related Topics
Java: How to Test Methods That Call System.Exit()
Java: Notify() VS. Notifyall() All Over Again
Why Does Changing the Returned Variable in a Finally Block Not Change the Return Value
How to Get the Current Time in Yyyy-Mm-Dd Hh:Mi:Sec.Millisecond Format in Java
What Is the Solution for the N+1 Issue in JPA and Hibernate
Java Date Format Conversion - Getting Wrong Month
Convert Java.Util.Date to What "Java.Time" Type
Parsing Iso-8601 Datetime with Offset with Colon in Java
Java Sending and Receiving File (Byte[]) Over Sockets
How to Start/Stop/Restart a Thread in Java
How to Compare Two Version Strings in Java
How Is the Java Memory Pool Divided
Jdbc Connection Failed, Error: Tcp/Ip Connection to Host Failed
Differencebetween "Class.Forname()" and "Class.Forname().Newinstance()"
Differencebetween Method Overloading and Overriding
How to Put Axis on a .Png File in Java
Java.Lang.Unsatisfiedlinkerror No *****.Dll in Java.Library.Path