0

Now, I have looked into this on StackOverflow, but none of the responses solve the problem which makes me believe this is new.

I have a Java Faces project. I have a index.xhtml file and a .java file. I run a debug on the project and it does get to the following lines in my code where i try to access my folder called questions. This folder does exist in my project. It is on the same level as Web Pages, Source Packages, Test Packages, and so on. Here are my lines of code to access my directory called "questions"

        File directory = new File("questions");
        //get all the files from a directory
        File[] fList = directory.listFiles(); 
        for (File file : fList){                //FAILS!
            System.out.println(file.getName());
        }

I am at a loss. All other topics tell me that the root folder is the project folder. This is where my questions folder resides. Yet, fList must be null and I don't understand what I am doing wrong in accessing this directory.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
DaveK
  • 544
  • 1
  • 6
  • 16
  • *This folder does exist in my project* and it probably only exists in your project, whereas what you are wanting is for it to be on your **classpath** which will be accessible when you are running the code. Alternatively access the folder as a fully pathed name – Scary Wombat Feb 13 '18 at 02:29
  • see also https://stackoverflow.com/questions/1844688/how-to-read-all-files-in-a-folder-from-java – Scary Wombat Feb 13 '18 at 02:34
  • Fully pathed does work, but this is specific to my environment. I would love for the path string to be relative so that it can be run on another person's machine. – DaveK Feb 13 '18 at 02:53
  • In that case you need to deploy this folder along with your classes and have it as part of your classpath – Scary Wombat Feb 13 '18 at 02:56
  • I followed this question in order to get the folder into my project. https://stackoverflow.com/questions/3718201/how-to-add-resources-to-classpath. Accessing it like I do above doesn't work. fList is NULL. As far as I know I thought this was adding it to "classpath" as the doc files point to this section as well. This tells me I am confused as either what it means to add to classpath (which seemed simple enough) or I am confused as to how to access a folder that is included in the classpath. Folder with files is present in my projects pane as from the beginning. – DaveK Feb 13 '18 at 13:26

0 Answers0