Accessing .dat file from within a Jar file
I am trying to access a data file from a public class, both of which are located within a JAR file. However, when I execute the 开发者_如何学JAVAjar on a Hadoop cluster, the system throws a FileNotFoundException. The bottom line is: is it possible to access resources within a Jar when running an application on a cluster, or does the resource need to be copied to the HDFS individually, and for either of the above, how would you go about implementing it?
Thanks!
Yes, if the JAR is in the CLASSPATH you can call getResourceAsStream()
using a class loader or servlet context to get a reference to an InputStream for that file.
You will NOT have access to the file path. You give a path relative to the CLASSPATH and the classloader finds the file. You can get the contents, but not the absolute location.
You shouldn't want the absolute location. What will you do with it?
You can't write to the file. You can't alter anything. If your app is packaged in a WAR file, you can't alter its contents.
精彩评论