getResource isn't working exported in a jar

So getResource does not work in a jar, or as I have it in my code it is returning the children of the file to null when I ask to list its files. In eclipse it works perfectly fine. I need it as a file, to list its children which are more files.

The / is relating to the class folder I have added.

Here is the block of code:

    try {
        mapFiles = (new File(LevelPickerScreen.class.getResource("/Maps").toURI())).listFiles();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
Jon Skeet
people
quotationmark

getResource works perfectly well - but you're then calling the File constructor, and the resource doesn't exist as a File. It's not a file - it's a resource within a jar file.

I suspect you'll find it hard to get it to list the resources within the jar file in this way, too - you could load the jar file separately, but it would probably be simpler just to automate listing all of the children as part of your build procedure, and then drop a file into the jar file as /Maps/maps.txt which contains a list of the map names. You can then load each of them in turn using getResource or getResourceAsStream - but don't try to use the File API with it.

people

See more on this question at Stackoverflow