77

While trying to copy some files in my jar file to a temp directory with my java app, the following exception is thrown:

java.nio.file.FileSystemNotFoundException
    at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
    at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
    at java.nio.file.Paths.get(Unknown Source)
    at com.sora.util.walltoggle.pro.WebViewPresentation.setupTempFiles(WebViewPresentation.java:83)
   ....

and this is a small part of my setupTempFiles(with line numbers):

81. URI uri = getClass().getResource("/webViewPresentation").toURI();
//prints: URI->jar:file:/C:/Users/Tom/Dropbox/WallTogglePro.jar!/webViewPresentation
82. System.out.println("URI->" + uri );
83. Path source = Paths.get(uri);

the webViewPresentation directory resides in the root directory of my jar:

enter image description here

This problem only exits when I package my app as a jar, debugging in Eclipse has no problems. I suspect that this has something to do with this bug but I'm not sure how to correct this problem.

Any helps appreciated

If matters:

I'm on Java 8 build 1.8.0-b132

Windows 7 Ult. x64

5
  • You have to do it slightly differently for jars stackoverflow.com/questions/5171957/access-file-in-jar-file Commented Mar 24, 2014 at 9:42
  • The method used in the answer reads a single file as Stream, I want to copy the entire directory. I've add a screenshot to clarify Commented Mar 24, 2014 at 9:46
  • Maybe you also want to use Files.createTempDirectory Commented Mar 24, 2014 at 10:11
  • Yes I did that somewhere in my main() Commented Mar 24, 2014 at 10:12
  • While running in your IDE (Eclipse) the resource is actualy a file. If you package the application and try to run it outside the IDE, the problem will surface. Commented Oct 5, 2020 at 15:18

4 Answers 4

68

A FileSystemNotFoundException means the file system cannot be created automatically; and you have not created it here.

Given your URI, what you should do is split against the !, open the filesystem using the part before it and then get the path from the part after the !:

final Map<String, String> env = new HashMap<>();
final String[] array = uri.toString().split("!");
final FileSystem fs = FileSystems.newFileSystem(URI.create(array[0]), env);
final Path path = fs.getPath(array[1]);

Note that you should .close() your FileSystem once you're done with it.

Sign up to request clarification or add additional context in comments.

14 Comments

Yes, that is something which is not very well documented... You should .resolve() or .relativize() the .toString() values of other paths if these paths are not from the same filesystem provider... I have been bitten by that too (I am developing a FileSystem implementation over FTP)
Not sure it is any better but here you go :)
Note that since then I have developed something that may interest you; see here
@8odoros you'd better use .replaceFirst(); you only want to replace the first occurrence you find, not all
Can this approach work with nested jars, e.g. jar:file:outer.jar!/inner.jar!/file.txt?
|
13

Accepted answer isn't the best since it doesn't work when you start application in IDE or resource is static and stored in classes! Better solution was proposed at java.nio.file.FileSystemNotFoundException when getting file from resources folder

InputStream in = getClass().getResourceAsStream("/webViewPresentation");
byte[] data = IOUtils.toByteArray(in);

IOUtils is from Apache commons-io.

But if you are already using Spring and want a text file you can change the second line to

StreamUtils.copyToString(in, Charset.defaultCharset());

StreamUtils.copyToByteArray also exists.

2 Comments

The OP was trying to get a Path (presumably for the new async processing capabilities in NIO), not a stream or a string, so this is not the answer.
It might not be the answer to the questions but indeed the path/uri solution is unusable for two reasons: It does not work if resources are unpacked and it does not work if you open two resources from the jar simultanously - you can open the file system only once ... however how do you know in general if it is the same file system? Lots of tests are necessary. The answer here shows a simple and working solution.
10

This is maybe a hack, but the following worked for me:

URI uri = getClass().getResource("myresourcefile.txt").toURI();

if("jar".equals(uri.getScheme())){
    for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
        if (provider.getScheme().equalsIgnoreCase("jar")) {
            try {
                provider.getFileSystem(uri);
            } catch (FileSystemNotFoundException e) {
                // in this case we need to initialize it first:
                provider.newFileSystem(uri, Collections.emptyMap());
            }
        }
    }
}
Path source = Paths.get(uri);

This uses the fact that ZipFileSystemProvider internally stores a List of FileSystems that were opened by URI.

1 Comment

It's weird Paths.get(URI) is not packed with the ability to deal with the jar file paths...
0

If you're using spring framework library, then there is an easy solution for it.

As per requirement we want to read webViewPresentation; I could solve the same problem with below code:

URI uri = getClass().getResource("/webViewPresentation").toURI();
FileSystems.getDefault().getPath(new UrlResource(uri).toString());

1 Comment

Doesn't seem to work with nested JARs or BOOT-INF/classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.