简体   繁体   English

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

[英]How to add resource file to JAR in IntelliJ

I am using IntelliJ IDEA 14 and I want to add file outside of src to the JAR file.我正在使用 IntelliJ IDEA 14,我想将 src 之外的文件添加到 JAR 文件中。 This is my current project structure.这是我目前的项目结构。

I want to add layout.txt and saveddata.txt to the JAR file executable.我想将 layout.txt 和 saveddata.txt 添加到 JAR 文件可执行文件中。 I've been googling on that for a while can't find the solution我已经在谷歌上搜索了一段时间找不到解决方案

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这是我的项目结构

布局结构

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

在此处输入图片说明

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

It's still for me.它仍然是给我的。 I tried:我试过:

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

For me, the resources directory was already marked as Resources Root but the content was missing in the jar .对我来说, resources目录已经被标记为Resources Root但内容在jar中丢失。 I had to manually add the resources dir to the jar artifact using the Project Structure window.我必须使用“ Project Structure窗口手动将resources目录添加到jar工件。

  1. Open Project Structures window打开Project Structures窗口
  2. Select Artifacts and click on the + button and then select Directory Content选择Artifacts并单击+按钮,然后选择Directory Content
  3. Choose resources directory选择resources目录
  4. Press Apply then OKApply然后OK

在此处输入图片说明

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

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