-1

I am trying to make a program that will get all files within a jar file, and then copy them. This is the code I am using to copying files:

 public static void copyFolder(File src, File dest)
    throws IOException{

    if(src.isDirectory()){

        //if directory not exists, create it
        if(!dest.exists()){
           dest.mkdir();
           System.out.println("Directory copied from " 
                          + src + "  to " + dest);
        }

        //list all the directory contents
        String files[] = src.list();

        for (String file : files) {
           //construct the src and dest file structure
           File srcFile = new File(src, file);
           File destFile = new File(dest, file);
           //recursive copy
           copyFolder(srcFile,destFile);
        }

    }else{
        //if file, then copy it
        //Use bytes stream to support all file types
        InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest); 

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes 
            while ((length = in.read(buffer)) > 0){
               out.write(buffer, 0, length);
            }

            in.close();
            out.close();
            System.out.println("File copied from " + src + " to " + dest);
    }
}

But there is an error - java.io.FileNotFoundException: (Access is denied) in OutputStream out = new FileOutputStream(dest);. Now, I have no idea why or what does that really mean? How can I fix it?

Plus, I have absolutely no idea how to extract files from a jar file. I have seen the ZipFile class but I don't really know how to use it... So that leaves me with 3 questions: 1. Whats wrong with the copying code? 2. What does Access is denied mean? 3. Can anyone give me a method for getting files from a jar file? Because jar.listFiles() returns an empty list.

Thanks in advance!

Raedwald
  • 46,613
  • 43
  • 151
  • 237
NonameSL
  • 27
  • 2
  • 9
  • 2
    Please paste the stack trace. – Shoaib Chikate Apr 17 '14 at 11:56
  • If you are using java version 7 it has built in file coping(Files.copy(src,dest,options). To analyse the access denied we will need more information as Shoaib asked. Also this might be a duplicate question: http://stackoverflow.com/questions/1529611/how-to-write-a-java-program-which-can-extract-a-jar-file-and-store-its-data-in-s – Ludger Apr 17 '14 at 11:58

1 Answers1

0

Can anyone give me a method for getting files from a jar file?

I've written some utility classes to work with JARs/ ZIPs based on the NIO.2 File API (the library is Open Source):

Maven:

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>softsmithy-lib-core</artifactId>  
    <version>0.4</version>  
</dependency> 

Tutorial:

http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html#ExtractJarResourceSample

Puce
  • 37,247
  • 13
  • 80
  • 152