jar file, it should be in the root), then access it using Thread.currentThread().getContextClassLoader().getResourceAsStream("file.txt") Place the file at the root (after extracting. class file), then access it using getClass().getResourceAsStream("file.txt") jar file, it should be in the same directory as. Place the file in directory structure matching your package name (after extracting. To access a file in a jar you have two options: Java Jar file: use resource errors: URI is not hierarchical.How do I read a resource file from a Java jar file?.This is explained well by the answers to: You cannot read the entries within a jar (a zip file) like it was a plain old File. The URI is not hierarchical occurs because the URI for a resource within a jar file is going to look something like this: file:/example.jar!/file.txt. Rather than trying to address the resource as a File just ask the ClassLoader to return an InputStream for the resource instead via getResourceAsStream: InputStream in = getClass().getResourceAsStream("/file.txt") īufferedReader reader = new BufferedReader(new InputStreamReader(in)) Īs long as the file.txt resource is available on the classpath then this approach will work the same way regardless of whether the file.txt resource is in a classes/ directory or inside a jar.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |