I have a Java application I have written as a Spark Streaming job which requires some text resources that I have included in the jar in a resources directory (using the default Maven directory structure). With unit tests I have no problem accessing these files but when I run my program with spark-submit I get a FileNotFoundException. How do I access files on the classpath in my JAR when running with spark-submit?
The code I am currently using to access my file looks roughly like this:
InputStream input;
try {
URL url = this.getClass().getClassLoader().getResource("my file");
if (url == null) {
throw new IOException("file does not exist");
}
String path = url.getPath();
input = new FileInputStream(path);
} catch(IOException e) {
throw new RuntimeException(e);
}
Thanks.
Note this is not a duplicate of Reading a resource file from within jar (which was suggested), because this code works when run locally. It only fails when run in a Spark cluster.
What you could do is to use getResourceAsStream() method with the directory path, and the input Stream will have all the files name from that dir. After that you can concat the dir path with each file name and call getResourceAsStream for each file in a loop.
When you build a java project and pack it into a jar (or a war), the files under the resources folder are included into the jar. These files may include configuration files, scripts and other resources needed during run time.
In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass().
I fixed this by accessing the resources directory a different (and significantly less silly) way:
input = MyClass.class.getResourceAsStream("/my file");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With