简体   繁体   English

getResource无法在jar中导出

[英]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. 所以getResource在jar中不起作用,或者因为我在我的代码中有它,当我要求列出其文件时,它会将文件的子节点返回null。 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();
    }

getResource works perfectly well - but you're then calling the File constructor, and the resource doesn't exist as a File . getResource运行得非常好 - 但是您正在调用File构造函数,并且该资源作为File存在。 It's not a file - it's a resource within a jar file. 它不是文件 - 它是jar文件中的资源。

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. 我怀疑你会发现很难让它以这种方式列出jar文件中的资源 - 你可以单独加载jar文件,但它可能更简单,只是自动列出所有的孩子作为一部分您的构建过程,然后将文件作为/Maps/maps.txt放入jar文件中,该文件包含地图名称列表。 You can then load each of them in turn using getResource or getResourceAsStream - but don't try to use the File API with it. 然后,您可以使用getResourcegetResourceAsStream依次加载它们 - 但不要尝试使用File API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM