Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从jar中获取资源(文件夹)?_Java_Jar_Resources_Directory - Fatal编程技术网

Java 如何从jar中获取资源(文件夹)?

Java 如何从jar中获取资源(文件夹)?,java,jar,resources,directory,Java,Jar,Resources,Directory,基本上,我要做的是解析一个目录中的所有文件,并将所有子项添加到一个列表。 问题是我没有主函数,我只运行一个jar。所以我不能这么做: URL url = MyClass.class.getResource("resources/"); if (url == null) { // error - missing folder } else { File dir = new File(url.toURI()); //throws error when running the jar

基本上,我要做的是解析一个目录中的所有文件,并将所有子项添加到一个列表。 问题是我没有主函数,我只运行一个jar。所以我不能这么做:

URL url = MyClass.class.getResource("resources/");
if (url == null) {
     // error - missing folder
} else {
    File dir = new File(url.toURI()); //throws error when running the jar
    for (File nextFile : dir.listFiles()) {
        // Do something with nextFile
    }
}
当它抛出IllegalArgument异常时。我还尝试通过以下方式获取InputStream:

InputStream inStream = CompilerUtils.class.getClassLoader().getResourceAsStream(filePath)

但它似乎不适用于文件。我正在寻找一种适用于目录的替代方法。

获取url url.getFile的字符串表示它看起来像文件:/C:/xxx/yyy/x.jar/资源从中提取路径:C:/xxx/yyy/x.jar。使用JarFile类读取文件夹