Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.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 - Fatal编程技术网

Java-读取jar中的文件

Java-读取jar中的文件,java,jar,Java,Jar,我知道有很多方法可以做到这一点,但是我在这项任务中遇到了一些问题,我无法用我已经找到的解决方案来解决这些问题 首先,我不想读取jar中的特定文件:我想读取给定目录路径的jar中包含的所有文件。 也就是说,通过一些研究,我发现了如何做到这一点,并编写了一个测试代码 public class StringLocalizer { private final List<URL> files; public StringLocalizer(final String stri

我知道有很多方法可以做到这一点,但是我在这项任务中遇到了一些问题,我无法用我已经找到的解决方案来解决这些问题

首先,我不想读取jar中的特定文件:我想读取给定目录路径的jar中包含的所有文件。 也就是说,通过一些研究,我发现了如何做到这一点,并编写了一个测试代码

public class StringLocalizer {

    private final List<URL> files;

    public StringLocalizer(final String stringsDirectory) throws URISyntaxException, IOException {

        ClassLoader loader = Thread.currentThread().getContextClassLoader();

        final BufferedReader br = new BufferedReader(new InputStreamReader(loader.getResourceAsStream(stringsDirectory), StandardCharsets.UTF_8));
        files = br.lines()
                .map(l -> stringsDirectory + "/" + l)
                .map(loader::getResource)
                .collect(Collectors.toList());

        // This line has the debug purpose of showing all the url contained in the list
        System.out.println(Arrays.toString(files.toArray(new URL[files.size()]))); 
    }

    public static void main(String[] args) {

        try {
            new StringLocalizer("test/testDirectory/");
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
    }

}
代码运行良好,这是我的第一个想法,但是当我试图将程序打包到一个可运行的JAR文件中时,我得到了一个意外的输出:

[]
为什么即使文件打包在JAR文件中,列表也是空的

(正确的输出应该是包含给定目录中所有文件的数组的表示形式,就像第一个输出一样)

编辑: 为了更好地了解我的情况,我有以下文件:

>MyJar.jar
    >classFiles
    >test>testDirectory>test.xml // I want to get this file
注意:这个目录将包含更多的文件,所以我不想静态地访问它们,但我想动态地读取所有文件

编辑: 使用ZipFile提取文件的代码:

public class StringLocalizer {

    private final List<ZipEntry> files = new ArrayList<ZipEntry>();

    public StringLocalizer(final String stringsDirectory) throws URISyntaxException, IOException {

        URL jarUrl = getClass().getProtectionDomain().getCodeSource().getLocation();
        File jarFile = new File(jarUrl.toURI().getPath());

        ZipFile unzipper = new ZipFile(jarFile, ZipFile.OPEN_READ);

        ZipEntry dirEntry = unzipper.getEntry(stringsDirectory);

        Enumeration<? extends ZipEntry> entries = unzipper.entries();

        for(ZipEntry entry = entries.nextElement(); entries.hasMoreElements(); entry = entries.nextElement()) {

            if(entry.getName().startsWith(dirEntry.getName()) && !entry.getName().equals(dirEntry.getName())) {
                files.add(entry);
            }
            System.out.println(entry.getName());
        }

        System.out.println(Arrays.toString(files.toArray(new ZipEntry[files.size()])));

        unzipper.close();
    }

    public static void main(String[] args) {

        try {
            new StringLocalizer("test/testDirectory/");
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
    }

}
公共类字符串定位器{
私有最终列表文件=新的ArrayList();
公共StringLocalizer(最终字符串stringsDirectory)抛出URI语法异常,IOException{
URL jarUrl=getClass().getProtectionDomain().getCodeSource().getLocation();
File jarFile=新文件(jarUrl.toURI().getPath());
ZipFile unzippers=新的ZipFile(jarFile,ZipFile.OPEN_READ);
ZipEntry dirEntry=unzippers.getEntry(stringsDirectory);

枚举JAR文件实际上是ZIP文件,所以只需像建议的那样将文件当作ZIP文件对待。

我知道这并不能解决您的问题,但我听说JAR资源的行为因JAR的运行方式而异。我强烈建议将您的资源与JAR分开打包,这样可以省去很多麻烦。Did您是否尝试打印stringsDirectory参数的值?另外,我假设您确认xml文件实际上在jar文件中?@AmirAfghani我已经使用WinRar手动提取了jar文件,并且我已经找到了xml文件的确切位置。我建议您使用Java内置ZipFile文件类,然后。他们会允许你打开一个jar,它实际上是一个zip。我有一个问题:一旦我得到目录的
ZipEntry
对象,我如何列出
ZipEntry
中的所有文件?我找到了如何得到正确的
ZipEntry
但是我认为我的代码中有一个问题,因为最后一个
ZipEntry被忽略:我正在添加问题中的新代码。在之后再循环一次怎么样?可能循环条件会在需要之前停止一次旋转。我已经尝试手动调用
条目。nextElement()
方法,但我得到的只是一个异常,表示没有更多的元素了……我也用Java8流API而不是循环解决了这个问题!
public class StringLocalizer {

    private final List<ZipEntry> files = new ArrayList<ZipEntry>();

    public StringLocalizer(final String stringsDirectory) throws URISyntaxException, IOException {

        URL jarUrl = getClass().getProtectionDomain().getCodeSource().getLocation();
        File jarFile = new File(jarUrl.toURI().getPath());

        ZipFile unzipper = new ZipFile(jarFile, ZipFile.OPEN_READ);

        ZipEntry dirEntry = unzipper.getEntry(stringsDirectory);

        Enumeration<? extends ZipEntry> entries = unzipper.entries();

        for(ZipEntry entry = entries.nextElement(); entries.hasMoreElements(); entry = entries.nextElement()) {

            if(entry.getName().startsWith(dirEntry.getName()) && !entry.getName().equals(dirEntry.getName())) {
                files.add(entry);
            }
            System.out.println(entry.getName());
        }

        System.out.println(Arrays.toString(files.toArray(new ZipEntry[files.size()])));

        unzipper.close();
    }

    public static void main(String[] args) {

        try {
            new StringLocalizer("test/testDirectory/");
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
    }

}