java 如何在 IntelliJ 中将资源文件添加到 JAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29887524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:04:50  来源:igfitidea点击:

How to add resource file to JAR in IntelliJ

javaintellij-idea

提问by Vincent_Paing

I am using IntelliJ IDEA 14 and I want to add file outside of src to the JAR file. This is my current project structure.

我正在使用 IntelliJ IDEA 14,我想将 src 之外的文件添加到 JAR 文件中。这是我目前的项目结构。

I want to add layout.txt and saveddata.txt to the JAR file executable. I've been googling on that for a while can't find the solution

我想将 layout.txt 和 saveddata.txt 添加到 JAR 文件可执行文件中。我已经在谷歌上搜索了一段时间找不到解决方案

In case you need to see my code. This is how I am reading file

如果您需要查看我的代码。这就是我读取文件的方式

private Path layoutPath = Paths.get("resources/layout.txt");
content = new String(Files.readAllBytes(layoutPath));

Here is my project structure

这是我的项目结构

Layout Structure

布局结构

回答by Brian Topping

  1. Create a folder called "resources" at the same level as "src"
  2. Right click the folder, select "Mark Directory As -> Resources Root"
  1. 在与“src”相同的级别创建一个名为“resources”的文件夹
  2. 右键单击文件夹,选择“将目录标记为 -> 资源根目录”

enter image description here

在此处输入图片说明

回答by Ranjeet

  • Make new directory with name as "resources" under your project root directory.
  • Right click on that directory and select "Mark Directory As" ==>"Resources Root" option.
  • 在项目根目录下创建名为“resources”的新目录。
  • 右键单击该目录并选择“将目录标记为”==>“资源根目录”选项。

回答by Kh?c Ngh?a T?

It's still for me. I tried:

它仍然是给我的。我试过:

+ "Mark Directory As" ==>"Resources Root"
+ getClassLoader().getResourceAsStream()
+ getClass().getClassLoader().getResource()
and Thread.currentThread().getContextClassLoader().getResourceAsStream()