0

Im using IntelliJ if this is relevant.

Im trying to open a folder(from a different package) as a file in order to get a list of all the files in the folder.

Here is the structure of my program: (both the wanted folder and the class where the code is written are highlighted)

Program structure

Here is the code where I try to open the folder as a File:

...
File emoteFolder = new File(getClass().getClassLoader().getResource("TwitchBotStadium/htmlFormater/emotes").getFile());
...

The problem is that emoteFolder is null, no matter what I put in the brackets of new File() nothing worked. I've tried:

File emoteFolder = new File(getClass().getResource("/TwitchBotStadium/htmlFormater/emotes").getFile());
File emoteFolder = new File("/TwitchBotStadium/htmlFormater/emotes");
File emoteFolder = new File("TwitchBotStadium/htmlFormater/emotes");
File emoteFolder = new File(HtmlMessageFormater.class.getResource("emotes/").getPath());
etc...

I've searched for a solution but couldn't find anything, I'm really close to just restructure the project but I still have some hope to find some help here.

artembus
  • 427
  • 1
  • 6
  • 13
  • When a project is packaged onto a `Jar`, it becomes a zip based file, so you can no longer access it contents using `File`, cause they're simply not files. `getClass().getResource("/TwitchBotStadium/htmlFormater/emotes")` will return you a `URL` to the named resource, but again, you can't simply "list" the contents, there's no real mechanisms available to do this dynamically. Normally, what I do, is include a text file which lists the contents of the package (usually generating it at build time) and read this file first (using `Class#getResource`) – MadProgrammer Jan 10 '16 at 23:39
  • I can't mark this question as a duplicate, but I think is duplicate of [link](http://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection) – Alejandro Goñi Jan 10 '16 at 23:52
  • The resources at runtime are those which have been compiled. It is the files which would go into a JAR if there was one. This should not be confused with the source `.java` files and directories which were used to build the code in the first place. These are not available at runtime. – Peter Lawrey Jan 11 '16 at 00:02
  • So is there is no way to get a list of files in that folder (I can access them and use them in the program so it's strange I cant list them)? if not what workaround can I implement? just make a text file before compiling the program? or is there more elegant solution? – artembus Jan 11 '16 at 12:21

0 Answers0