简体   繁体   English

在 JAR 文件中获取 src 文件夹之外的资源

[英]Getting resources outside of src folder in a JAR file

I've been searching around for hours trying to fix this problem and found multiple posts about similar problems, but nothing I find seems to do the exact thing I'm trying.我已经搜索了几个小时试图解决这个问题,并找到了多篇关于类似问题的帖子,但我发现似乎没有做我正在尝试的确切事情。

I have a res folder at the same level of my src folder which I use for things like images and text files.我在src文件夹的同一级别有一个res文件夹,我将其用于图像和文本文件之类的内容。 I can easily spit out the text file in Eclipse by simply doing:只需执行以下操作,我就可以轻松地在 Eclipse 中吐出文本文件:

    FileInputStream fis = new FileInputStream("res/misc/foo.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));

    String line;
    while((line = br.readLine()) != null) {
        System.out.println(line);
    }

However, the problem starts when I package this up in a runnable JAR file.但是,当我将它打包到一个可运行的 JAR 文件中时,问题就开始了。 I found multiple threads pointing this out:我发现多个线程指出了这一点:

Class.class.getResourceAsStream("/misc/foo.txt");

However, this only works if the resources are in the same source folder as the class.但是,这仅在资源与类位于同一source文件夹中时才有效。 And indeed, when i move the res folder into src it can find the resource, but that's not how I want to organize my project.事实上,当我将res文件夹移动到src它可以找到资源,但这不是我想要组织我的项目的方式。

Is there some way to do this that I missed, or should I change my folder structure?有什么方法可以做到这一点,我错过了,还是应该更改我的文件夹结构?

You should be able to get the path with something like:您应该能够使用以下内容获取路径:

String path = <YourClassName>.class.getProtectionDomain().getCodeSource().getLocation().getPath();

Then a quick test to check if the directory is there:然后快速测试以检查目录是否存在:

File folder = new File(path);
File[] dirList = folder.listFiles();

for (File list : dirList) {
    if (list.isDirectory()) {
        System.out.println("Directory " + list.getName());
    }
}

You're going to have to change your structure, such as make res a source folder as well (you can have more than one source folder).您将不得不更改您的结构,例如将res设为源文件夹(您可以拥有多个源文件夹)。 It's not really about being a source folder, it's about getting your resources to be copied into the same tree that makes up the contents of the resulting jar.这不是真正的一个源文件夹,它是关于让你的资源被复制到同一棵树,构成了所产生的罐子的内容。

Just likenitind already said:就像nitind已经说过的:

Create the folder res as a source folder (just like src ).创建文件夹res作为源文件夹(就像src )。 Put your files there and then you can access them the same way you access files in your src ie将您的文件放在那里,然后您就可以像访问src文件一样访问它们,即

FileInputStream fis = new FileInputStream("/misc/foo.txt");

In Eclipse you can simply create a new file as "Source Folder".在 Eclipse 中,您可以简单地创建一个新文件作为“源文件夹”。 You could also just copy the scr folder and rename it to lets say res .您也可以复制scr文件夹并将其重命名为res

Use maven resource plugin, The resources plugin copies files from input resource directories to an output directory.使用 maven 资源插件,资源插件将文件从输入资源目录复制到输出目录。

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
        ...
    </configuration>
</plugin>

Assume we want to copy resource files from the directory input-resources to the directory output-resources and we want to exclude all files ending with the extension .png.假设我们要将资源文件从目录 input-resources 复制到目录 output-resources,并且我们想要排除所有以扩展名 .png 结尾的文件。

just like excludes you can include files using就像排除一样,您可以使用包含文件

These requirements are satisfied with this configuration:此配置满足以下要求:


<configuration>
    <outputDirectory>output-resources</outputDirectory>
    <resources>
        <resource>
            <directory>input-resources</directory>
            <excludes>
                <exclude>*.png</exclude>
            </excludes>
            <filtering>true</filtering>
        </resource>
    </resources>
</configuration>

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

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