0

I am trying to make a JAR write files from it's resources to a directory in the local directory. When I run it from NETBEANS it works perfectly. When I run it from the JAR in the DIST folder it's just not working anyway I've tried to reference it...

Here's the relevant part of the code:

for(File directory:sampleDirectories)
            {
                dir = new File("presentaciones/"+directory.getName());
                dir.mkdir();
                try {
                    br = new BufferedReader(new FileReader(directory.getAbsolutePath()+"/"+directory.getName()+".txt"));
                    bw = new BufferedWriter(new FileWriter(dir.getPath()+"/"+directory.getName()+".txt"));
                    c = br.readLine();
                    while (c != null) {
                        bw.write(c);
                        bw.write("\n");
                        c = br.readLine();
                    }
                    br.close(); bw.close();
                } catch (IOException ex) {
                    Logger.getLogger(Opinarium3.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
hurricane
  • 6,521
  • 2
  • 34
  • 44
Alexis R Devitre
  • 288
  • 2
  • 6
  • 21

2 Answers2

1
  1. Try File#mkdirs instead of File#mkdir.
  2. Embedded resources can't be accessed via a File reference. Instead you need to use Class#getResource or Class#getResourceAsStream
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks for the input, I have tried to create only the directory structure super/sub/ in the local directory and I have found that it doesn't even do that so now I am wondering if I am using mkdirs properly... In my former code, I actually used mkdir twice... once before the FOR to create super ("presentaciones") then another time in the FOR to create each one of the subdirectories before placing copying files to the subdirectories which was done in an inappropriate manner as you pointed out – Alexis R Devitre Aug 15 '14 at 20:53
  • mkdir & mkdirs return Boolean, you can use this to determine if the call succeeded or not. You could also use File#getAbsolutePath and dump this to System.out to see where the directory was made – MadProgrammer Aug 15 '14 at 21:33
  • Yes!! Thank you for your input again!! It did not answer my question directly but put me on the right path to find was I was looking which can be found here [link](http://stackoverflow.com/questions/1429172/how-do-i-list-the-files-inside-a-jar-file) – Alexis R Devitre Aug 15 '14 at 22:06
  • What might be easier is to write a known file at build time, which lists all the files in the directory which you want to extract. Then at runtime, read this file (via getResource), and use it's content to find the resources to be extracted – MadProgrammer Aug 16 '14 at 06:51
0

My actual mistake, as @MadProgrammer pointed out was in the way I read embedded ressources. If anyone else is interested in reading file names from embedded ressources, the key to doing this can be found here Stack Overflow Article: How do I list the files inside a JAR file?

Community
  • 1
  • 1
Alexis R Devitre
  • 288
  • 2
  • 6
  • 21