简体   繁体   中英

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. This is my current project structure.

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

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"
  2. Right click the folder, select "Mark Directory As -> Resources Root"

在此处输入图片说明

  • 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.

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 . I had to manually add the resources dir to the jar artifact using the Project Structure window.

  1. Open Project Structures window
  2. Select Artifacts and click on the + button and then select Directory Content
  3. Choose resources directory
  4. Press Apply then OK

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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