1

Hi I am stuck with following code. I would like to get files from folder but when I export project to jar file it wont work any idea?

 public String getXSDfilenames() {
        String filenames= "";
        try {
            File currDir = new File(".");
            String path = currDir.getAbsolutePath();
            path = path.substring(0, path.length()-1);

             File file = new File(path+"src\\schemaFiles");     
            String[] files = file.list(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith(".xsd");
                }
            });
            if (file.exists()) {
                for (int i = 0; i < files.length; i++) {
                    System.out.println(path+"src\\schemaFiles\\"+files[i]);
                filenames = filenames + files[i] + newline;
                }
            } else {
                System.out.println("No schema files founded in default folder!");
            }
        }
         catch (Throwable e1) {
        System.out.println(e1);
        }
        return filenames;
    }

}
Buteo Mausjäger
  • 123
  • 1
  • 11

1 Answers1

0

If you change the name of your XXX.jar file to XXX.zip, then open it in whatever Zip file utility you might have available, you can look inside the file and see 1. Whether your files are getting included in the jar at all, and if they are, 2. the actual path in which they are stored. I strongly suspect there's now src folder inside your jar.

Also, if you're NOT expecting the files to be in the jar, then you'll probably need to supply an absolute path to the files.

If they are in the jar, then check out these questions:

load file within a jar,

How to use ClassLoader.getResources() correctly?,

How do I list the files inside a JAR file?

Community
  • 1
  • 1
StormeHawke
  • 5,987
  • 5
  • 45
  • 73
  • Yes they are included so what I need to do is only change path to jar. file and it should work? – Buteo Mausjäger Aug 23 '13 at 14:15
  • If they are in the jar, then check out these questions: http://stackoverflow.com/questions/4548699/load-file-within-a-jar, http://stackoverflow.com/questions/5193786/how-to-use-classloader-getresources-correctly/5194049#5194049, http://stackoverflow.com/questions/1429172/how-do-i-list-the-files-inside-a-jar-file – StormeHawke Aug 23 '13 at 14:32
  • This link might also be useful: http://docs.oracle.com/javase/tutorial/deployment/jar/view.html – StormeHawke Aug 23 '13 at 14:34
  • 1
    Thank you its working as you said @StormeHawke and I ll check it :) – Buteo Mausjäger Aug 23 '13 at 14:34