0

I am attempting to store some resources for my program within the runnable jar created through Intellij and then extract those files at runtime after receiving some user input. The folders are located on the root of the jar. I have got it to successfully extract the files as intended but the problems begin when the jar is renamed. As in applic1.jar to applic2.jar. Does anyone know why it is behaving in this manner?

This is the method that performs the extractions. Modified version of: How can I get a resource "Folder" from inside my jar File?

File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
    boolean inDirectory = false;

        if (jarFile.isFile()) { //run in JAR
            try {
                JarFile jar = new JarFile(jarFile);
                Enumeration<JarEntry> entries = jar.entries();
                while (entries.hasMoreElements()) {
                    JarEntry currentJar = entries.nextElement();
                    String name = currentJar.getName();
                    Path currentFile = Paths.get(name).getFileName();
                    if (name.startsWith(oldPath.replaceAll(Pattern.quote("\\"), "/"))) {

                        if (currentJar.isDirectory()) {
                            Files.createDirectories(Paths.get(newPath));

                            inDirectory = true;
                        } else {
                            if (Files.notExists(Paths.get(newPath), LinkOption.NOFOLLOW_LINKS)) {

                                Files.createDirectories(Paths.get(newPath).getParent());

                            }
                            BufferedInputStream source = new BufferedInputStream(jar.getInputStream(currentJar));
                            File dest = new File(newPath + (inDirectory ? "\\" + currentFile : ""));
                            FileOutputStream fos = new FileOutputStream(dest);
                            int read;
                            while ((read = source.read()) != -1) {
                                fos.write(read);
                                fos.flush();
                            }
                            fos.close();
                            source.close();
                        }
                    }
                }
                jar.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else { //run in IDE
            copyAndRename(oldPath, newPath);
        }
daro91
  • 3
  • 4
  • What is the purpose of extracting your entire .jar file? If you just read entries with [Class.getResource](https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#getResource-java.lang.String-), you can throw away all that code that works with files and jar entries, and you won’t have to worry about the location of your .jar… or even whether the .jar is a local file at all. – VGR Apr 26 '18 at 22:26
  • I need to extract 2 folders and their contents, not the entire .jar file. And I didn't want to have to specify which files but instead target the folders which I couldn't do with getResource. I actually narrowed down my issue since posting this. It turns out that renaming is not issue but the location of the file. The `if (jarFile.isFile())` part returns false if the file is located in my onedrive folder. The path has spaces in it. – daro91 Apr 26 '18 at 22:52
  • `getLocation().getPath()` *does not* convert a URL into a valid file name. It just returns the path portion of the URL, which may contain percent-escapes for the many characters which are not allowed to appear directly in a URL, such as spaces. The correct way to convert a URL to a file name is `new File(location.toURI())`. – VGR Apr 26 '18 at 22:56
  • That did the trick. Everything is working perfectly now. Thanks @VGR – daro91 Apr 27 '18 at 17:44

0 Answers0