javaeclipsefileprogram-files

How to copy resources folder out of jar into program files


I have spent hours and hours searching for the answer and I just can't figure it out, I am trying to copy my resources folder which contains all the images and data files for my game I am working on out of the running jar and into E:/Program Files/mtd/ It works fine when I run it out of eclipse, but when I export the jar and try it, I get NoSuchFileException

`JAR
Installing...
file:///C:/Users/Cam/Desktop/mtd.jar/resources to file:///E:/Program%20Files/mtd
/resources
java.nio.file.NoSuchFileException: C:\Users\Cam\Desktop\mtd.jar\resources
        at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
        at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
        at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
        at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(Unknown Sou
rce)
        at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(Unknown Sou
rce)
        at sun.nio.fs.WindowsFileSystemProvider.readAttributes(Unknown Source)
        at java.nio.file.Files.readAttributes(Unknown Source)
        at java.nio.file.FileTreeWalker.walk(Unknown Source)
        at java.nio.file.FileTreeWalker.walk(Unknown Source)
        at java.nio.file.Files.walkFileTree(Unknown Source)
        at java.nio.file.Files.walkFileTree(Unknown Source)
        at me.Zacx.mtd.main.Game.<init>(Game.java:94)
        at me.Zacx.mtd.main.Game.main(Game.java:301)`

This is the code I am using:

    if (!pfFolder.exists()) {
    pfFolder.mkdir();
    try {

        URL url = getClass().getResource("/resources/");
        URI uri = null;

        if (url.getProtocol().equals("jar")) {
            System.out.println("JAR");
            JarURLConnection connect = (JarURLConnection) url.openConnection();
            uri = new URI(connect.getJarFileURL().toURI().toString() + "/resources/");

        } else if (url.getProtocol().equals("file")) {
            System.out.println("FILE");
            uri = url.toURI();
        }

        final Path src = Paths.get(uri);
        final Path tar = Paths.get(System.getenv("ProgramFiles") + "/mtd/resources/");

        System.out.println("Installing...");
        System.out.println(src.toUri() + " to " + tar.toUri());

        Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
            public FileVisitResult visitFile( Path file, BasicFileAttributes attrs ) throws IOException {
                return copy(file);
            }
            public FileVisitResult preVisitDirectory( Path dir, BasicFileAttributes attrs ) throws IOException {
                return copy(dir);
            }
            private FileVisitResult copy( Path fileOrDir ) throws IOException {
                System.out.println("Copying " + fileOrDir.toUri() + " to " + tar.resolve( src.relativize( fileOrDir ) ).toUri());
                Files.copy( fileOrDir, tar.resolve( src.relativize( fileOrDir ) ) );
                return FileVisitResult.CONTINUE;
            }
        });
        System.out.println("Done!");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

Solution

  • I FINALLY FOUND THE ANSWER I don't want to type out a big, long explanation but for anyone looking for the solution, here it is

     `  
                  //on startup
                   installDir("");
                    for (int i = 0; i < toInstall.size(); i++) {
                        File f = toInstall.get(i);
                        String deepPath = f.getPath().replace(f.getPath().substring(0, f.getPath().lastIndexOf("resources") + "resources".length() + 1), "");
                        System.out.println(deepPath);
                        System.out.println("INSTALLING: " + deepPath);
                        installDir(deepPath);
                        System.out.println("INDEX: " + i);
                    }
    
    public void installDir(String path) {
                System.out.println(path);
                final URL url = getClass().getResource("/resources/" + path);
                if (url != null) {
                    try {
                        final File apps = new File(url.toURI());
                        for (File app : apps.listFiles()) {
                            System.out.println(app);
                                System.out.println("copying..." + app.getPath() + " to " + pfFolder.getPath());
                                String deepPath = app.getPath().replace(app.getPath().substring(0, app.getPath().lastIndexOf("resources") + "resources".length() + 1), "");
                                System.out.println(deepPath);
    
                            try {
    
                                    File f = new File(resources.getPath() + "/" + deepPath);
                                    if (getExtention(app) != null) {
                                    FileOutputStream resourceOS = new FileOutputStream(f);
                                    byte[] byteArray = new byte[1024];
                                    int i;
                                    InputStream classIS = getClass().getClassLoader().getResourceAsStream("resources/" + deepPath);
                            //While the input stream has bytes
                                    while ((i = classIS.read(byteArray)) > 0) 
                                    {
                            //Write the bytes to the output stream
                                        resourceOS.write(byteArray, 0, i);
                                    }
                            //Close streams to prevent errors
                                    classIS.close();
                                    resourceOS.close();
                                    } else {
                                        System.out.println("new dir: " + f.getPath() + " (" + toInstall.size() + ")");
                                        f.mkdir();
                                        toInstall.add(f);
                                        System.out.println(toInstall.size());
                                    }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (URISyntaxException ex) {
                        // never happens
                    }
                }
    
            }`