The file is under src/main/resources/exampleFolder/exampleFile.extension
File database = new File(getClass().getClassLoader().getResource("exampleFolder/exampleFile.extension").getFile());
My application runs file because Eclipse IDE includes that under that cp but when I'm running the packaged jar it is not reading the file.
Please help
Comment From: jnizet
Hi @ohbus .
Your code doesn't work simply because you're trying to access your resource, which is bundled inside a jar file, as a File
. A File
represents... a file on the file system. You can't use a File
to access a resource bundled inside a jar, because... it's not in the file system.
Use getClass().getClassLoader().getResourceAsStream("exampleFolder/exampleFile.extension")
to get an InputStream
that you can read from. That will work whether or not your resource is bundled inside the jar.
You should ask such questions on StackOverflow rather than opening an issue. But search for existing questions first, because that question has been asked many many times already.
Comment From: ohbus
Hi @ohbus .
Your code doesn't work simply because you're trying to access your resource, which is bundled inside a jar file, as a
File
. AFile
represents... a file on the file system. You can't use aFile
to access a resource bundled inside a jar, because... it's not in the file system.Use
getClass().getClassLoader().getResourceAsStream("exampleFolder/exampleFile.extension")
to get anInputStream
that you can read from. That will work whether or not your resource is bundled inside the jar.You should ask such questions on StackOverflow rather than opening an issue. But search for existing questions first, because that question has been asked many many times already.
No I'm not trying to access a jar, instead I'm trying to access a file. It is running smoothly in my IDE but I can't make it run from the bundled jar which maven makes.
Personal opinion, Stack overflow answers did not help and when I post something it was very toxic. I love this GitHub community.
Comment From: jnizet
Hi @ohbus .
Please read my suggestionn again. I never claimed you were trying to access a jar. I said that you were trying to access a classpath resource, which is bundled inside a jar file. And using getResourceAsStream
as I showed in my previous answer is the right way to access a classpath resource, whether or not it's bundled in a jar file (so it will work both in your IDE, and when the app is bundled as a jar file).
Comment From: ohbus
@jnizet
Can you please help me out. I cannot understand how to do this.
IPLocationFinderServiceImpl() {
try {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("IPLocationDB/GeoLite2-City.mmdb");
File database = null;
try(OutputStream outputStream = new FileOutputStream(database)) {
IOUtils.copy(inputStream, outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//File database = new File(getClass().getClassLoader().getResource("IPLocationDB/GeoLite2-City.mmdb").getFile());
dbReader = new DatabaseReader.Builder(database).build();
}
catch(Exception e) {
e.printStackTrace();
}
}
Comment From: wilkinsona
@ohbus Unfortunately, this sort of back-and-forth isn't well-suited to the issue tracker as it creates a lot of notifications for everyone watching the repository. If you'd like to have a discussion, Gitter is a better place to have it. Alternatively, it you have a question, the spring-boot
tag on Stack Overflow is a good option.