【问题标题】:cannot get folder from src/main/resources Java project Maven无法从 src/main/resources Java 项目 Maven 获取文件夹
【发布时间】:2020-06-02 20:39:32
【问题描述】:

我尝试遍历资源中某个文件夹中的所有文件(我自己在 Eclipse 中通过执行 new->folder 添加了这个文件夹)。无论我尝试什么,它都会在我尝试获取文件夹的行上给出一个空异常。因为我需要遍历其内容,将其作为流或某些东西不起作用。 我尝试了很多方法,但是例如这两个给了我错误:

File folder = new File(Thread.currentThread().getContextClassLoader().getResource("schematics").getPath());

这个包裹在 try catch 中也不起作用:

File folder = new File(getClass().getClassLoader().getResource("schematics").toURI());

我的文件结构(我尝试访问原理图的类是 HeavenStructureManager 的初始化程序):

编辑 这个项目是一个 minecraft 插件,由 maven 捆绑到一个 jar 中,并由 spigotmc minecraft jar 运行。他们的 api 提供了一个函数 getResource() 但它提供了一个 InputStream。因为我想动态获取对我不起作用的文件夹中的所有文件。 我解压缩了我的 jar 文件以检查原理图在哪里,它看起来像这样:

【问题讨论】:

  • 您是在 Eclipse 中运行代码还是在单元测试中运行代码?当前目录也可能是项目目录。所以,应该是 src/main/resources/schematics 什么的。
  • @Serge 这是一个我的世界插件。我通过 maven 将它编译为 jar,但它是由 spigot 的 minecraft 服务器启动器运行的
  • 你能看到 System.getProperty("user.dir") (当前目录)的输出是什么吗?
  • @Serge 给出了/Users/Eric/Desktop/Test Server,这是我的服务器的 spigot 主 jar 所在的路径。这确实完全关闭,因为我试图从我的插件的 jar (jar of我的插件位于/Users/Eric/Desktop/Test Server\plugins\Heaven-0.1.jar
  • Heaven-0.1.jar 在正在运行的应用程序的类路径上?

标签: java eclipse


【解决方案1】:

下面是一个代码示例,用于遍历 jar 文件的内容,列出它们并将内容写入新文件。

当前运行目录:.

jar文件目录:./plugins/org.alloytools.alloy.dist.jar

输出目录:./unzipped

package com.serge;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

public class JarFileReader {
    public static void main(String[] args) {

        try {
            File file = new File("plugins/org.alloytools.alloy.dist.jar");
            FileInputStream fileInputStream = new FileInputStream(file);
            JarInputStream jarInputStream = new JarInputStream(fileInputStream);
            FileOutputStream outputStream = null;

            File unzippedDir = new File("unziped");

            while(jarInputStream.getNextEntry() != null) {
                JarEntry entry = jarInputStream.getNextJarEntry();

                // list the contents of the jar file 
                System.out.println(entry.toString());

                // write contents of a file within the jar file to a new file
                if("help/Nav.html".equals(entry.getName())) {
                    new File(unzippedDir + "/" + entry.getName().substring(0, entry.getName().indexOf("/"))).mkdir();
                    File unzippedFile = new File(unzippedDir + "/" + entry.getName());
                    unzippedFile.createNewFile();
                    outputStream = new FileOutputStream(unzippedFile);

                    byte[] bytes = new byte[1024];

                    int length = 0;

                    while((length = jarInputStream.read(bytes)) > 0) {
                        outputStream.write(bytes, 0, length);   
                    }
                }
            }

            outputStream.close();
            jarInputStream.close();
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

【讨论】:

  • 谢谢。然而,我只是接受了这样一个事实,即我只能以流的形式获取文件(设法改变了我使用 API 的方式)并且有一个告诉文件名是什么的列表。您的代码可能也可以工作,但为了稳定性/简单性,我将保留我现在拥有的。我会接受你的回答,因为你帮了我很多!
【解决方案2】:

尝试使用绝对路径:

getClass().getClassLoader().getResource("/schematics")

当你使用getResource方法时,getResource会尝试寻找相对于类包的资源。因此,当您使用路径(“schematics”)时,实际上,您指的是位于 [resources]/com/ericdebouwer/heaven/schematics 位置的文件。通过使用前导 / getResource 将其视为绝对路径。

【讨论】:

  • 这给了我一个java.lang.IllegalArgumentException: URI 不是分层错误。顺便说一句,我做到了:File schematicFolder = new File(getClass().getResource("/schematics").toURI()); 我应该如何解决这个问题?
  • 使用 getResourceAsStream 而不是尝试将其作为文件加载。
  • @Deadron 正如我在我的问题中所说,这不是一个选项。我需要遍历文件夹的内容,而我正在使用的库需要一个实际的文件对象。
  • 刚刚添加了一个代码示例来检索 jar 文件的内容(S:如下)。希望它提供一个想法。
猜你喜欢
  • 1970-01-01
  • 2018-05-13
  • 2013-05-11
  • 2016-10-12
  • 2015-05-07
  • 2015-02-26
  • 2012-05-06
  • 2018-02-07
相关资源
最近更新 更多