0

I have a jar compiled with the input files needed to run the jar within the jar itself. Question is, how do I specify the path so that the files can be read?

This is how I try to read the files:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
    classLoader = getClass().getClassLoader();
}
System.out.println("FILE is readable? -> " + new File(classLoader.getResource(strFileName).toExternalForm()).canRead());

List<String> lines = Files.readAllLines(new File(classLoader.getResource(strFileName).toExternalForm()).toPath());

But always, I get the message that the file is not readable. I know the file is in there because I've checked. I also know the classloader is finding the file because if I give it the file input.txt as strFileName, the path it produces points to within the jar and is similar to: jar:file:/home/me/Dropbox/Assignments/Files/MyJar.jar!/input.txt. Because of this, I know it finds the file, but then some io error occurs, and I'm just not sure what the problem is.

java.nio.file.NoSuchFileException: jar:file:/home/me/Dropbox/Assignments/Files/MyJar.jar!/input.txt
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
    at java.nio.file.Files.newByteChannel(Files.java:361)
    at java.nio.file.Files.newByteChannel(Files.java:407)
    at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
    at java.nio.file.Files.newInputStream(Files.java:152)
    at java.nio.file.Files.newBufferedReader(Files.java:2784)
    at java.nio.file.Files.readAllLines(Files.java:3202)
    at java.nio.file.Files.readAllLines(Files.java:3242)
    at com.smac89.school.Program.readState(GameState.java:203)
    at com.smac89.school.Program.simulate(GameState.java:57)
    at com.smac89.school.Program.main(GameState.java:31)

How do I do this? I have tried all manner of tricks and I still can't get the program to read the file.

smac89
  • 39,374
  • 15
  • 132
  • 179
  • Possible duplicate of [How do I load a file from resource folder?](http://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder) – BCartolo Mar 29 '17 at 17:33
  • 1
    Can you use `getResourceAsStream()` instead, combined with e.g. http://stackoverflow.com/questions/29282457/equivalent-to-files-readalllines-for-inputstream-or-reader if you require the individual lines? – Oliver Charlesworth Mar 29 '17 at 17:33
  • @BCartolo, that is definitely not the issue as I can load the file from the resource folder just fine. Only when I package it as a jar do I encounter this problem – smac89 Mar 29 '17 at 17:34
  • 2
    It **is** the issue. A resource inside a jar is not a file. It doesn't live on the file system. So you can't use file IO to read it. – JB Nizet Mar 29 '17 at 17:38
  • @OliverCharlesworth, thanks mate. Yes that worked! – smac89 Mar 29 '17 at 17:44
  • @JBNizet, Thank you, I missed that part when crafting the reader. I reasoned that since `Files.readAllLines` uses a BufferedReader to read the file, that this must be ok to do – smac89 Mar 29 '17 at 17:45
  • @BCartolo, I guess you're right, that actually was the issue – smac89 Mar 29 '17 at 17:46

0 Answers0