简体   繁体   English

无法从 src/main/resources Java 项目 Maven 获取文件夹

[英]cannot get folder from src/main/resources Java project Maven

I try to loop over all files in a certain folder in my resources (I added this folder myself in eclipse by doing new->folder).我尝试遍历资源中某个文件夹中的所有文件(我自己通过执行 new->folder 在 eclipse 中添加了这个文件夹)。 Whatever I try it keeps giving a null exception on the line where I try to get the folder.无论我尝试什么,它都会在我尝试获取文件夹的行上不断给出 null 异常。 As I need to loop over its content getting it as a stream or something won't work.因为我需要遍历其内容,将其作为 stream 或某些东西不起作用。 I tried many approaches, but for example these two gave me the error:我尝试了很多方法,但是例如这两个给了我错误:

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

This wrapped inside a try catch did also not work:这包裹在 try catch 中也不起作用:

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

My file structure (class where I try to access the schematics is HeavenStructureManager's initialiser):我的文件结构(我尝试访问原理图的类是 HeavenStructureManager 的初始化程序):

在此处输入图像描述

EDIT This project is a minecraft plugin, bundled to a jar by maven and run by the spigotmc minecraft jar.编辑这个项目是一个 minecraft 插件,由 maven 捆绑到 jar 并由 spigotmc minecraft jar 运行。 Their api provides a function getResource() but that gives a InputStream.他们的 api 提供了一个 function getResource() 但它提供了一个 InputStream。 As I would like to dynamically get all files inside a folder that does not work for me.因为我想动态获取对我不起作用的文件夹中的所有文件。 I unpacked my jar file to check where the schematic is, and it looked like this:我解压了我的 jar 文件以检查原理图在哪里,它看起来像这样: 在此处输入图像描述

Try using an absolute path:尝试使用绝对路径:

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

When you use the getResource method, getResource will try to find the resource relative to the class package.当您使用 getResource 方法时,getResource 将尝试查找相对于 class package 的资源。 Therefore, when you use the path ("schematics"), in reality, you are referring to the file at the location [resources]/com/ericdebouwer/heaven/schematics.因此,当您使用路径(“schematics”)时,实际上您指的是位于 [resources]/com/ericdebouwer/heaven/schematics 位置的文件。 By using a leading / getResource treats it as an absolute path.通过使用前导 /getResource 将其视为绝对路径。

Below is a code example to iterate through the contents of a jar file, list them and write the contents to a new file.下面是一个代码示例,用于遍历 jar 文件的内容,列出它们并将内容写入新文件。

Current run directory: .当前运行目录:.

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

Output directory: ./unzipped Output 目录:./解压

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();
        }
    }
}

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

相关问题 从Maven项目资源文件夹(src / main / resources)加载excel(.xlsx / .xls)文件 - Loading an excel(.xlsx/.xls) file from maven project resources folder (src/main/resources) Eclipse-Maven:更新项目从构建路径中排除 src/main/resources 文件夹 - Eclipse-Maven: Updating project excludes src/main/resources folder from build path 无法从src / main / resources文件夹Java 8中读取文件 - Cant read files from src/main/resources folder Java 8 Maven项目:Spring无法使用PropertyPlaceholderConfigurer在src / main / resources中找到属性文件 - Maven Project: Spring cannot find property file inside src/main/resources using PropertyPlaceholderConfigurer 如何从Java获取src / main / resources路径 - How to get src/main/resources path from Java Maven项目中src / main / java和src / test / java之间的区别 - Difference between src/main/java and src/test/java in a Maven project Maven项目不会将文件放在classpath的src / main / resources中 - Maven project doesn't put files in src/main/resources on classpath 如何包含来自 src/main/java 的资源 - How to include resources from src/main/java Eclipse 中 Maven 项目的类路径 - src vs src/main/java - Classpath for Maven project in Eclipse - src vs src/main/java 我的 eclipse maven 项目中没有 java 资源文件夹 - There is no java resources folder in my eclipse maven project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM