How to add resource file to JAR in IntelliJ
Asked Answered
D

4

15

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

Layout Structure

Dilorenzo answered 27/4, 2015 at 5:0 Comment(0)
A
17
  1. Create a folder called "resources" at the same level as "src"
  2. Right click the folder, select "Mark Directory As -> Resources Root"

enter image description here

Aurita answered 27/4, 2015 at 5:3 Comment(8)
Still giving me NoSuchFileException. I marked it as Resource Root.Dilorenzo
Presumably you mean your application is giving you an NSFE. Separate question if so, please post your code. General form is getClass().getResource("/path/image.png").Aurita
I have added the code. Can you check it again? Thanks :DDilorenzo
Try putting a "/" in front of the path and removing the folder "resources". The contents of your resources folder in the build is considered root, so your resource path would be "/layout.txt".Aurita
I have tried putting in root directory. It doesn't add to the JAR file alsoDilorenzo
If it's not in the JAR, you're going to get a NSFE no matter what the name, but it needs to be "/layout.txt" for the location you provided. You probably need to set up your Artifacts in Project Settings and be sure "Build on make" is selected.Aurita
Let us continue this discussion in chat.Dilorenzo
from jetbrains; "During the build process, by default, the resources are copied into the root of the compilation output folder. If necessary, you can specify a different folder within that output folder." you need to follow this page for solution; jetbrains.com/help/idea/2016.1/…Swindle
C
5
  • 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.
Ceramist answered 27/4, 2015 at 5:7 Comment(3)
Still giving me NoSuchFileException after I have rebuildedDilorenzo
@Renges : The code is using a relative path, which makes it depend on the current working directory of the JVM that launches the tests. A more reliable way to read resource files is 'getClass().getClassLoader().getResourceAsStream()', or, if necessary, 'getClass().getClassLoader().getResource()'. In a J2EE environment you'd typically use 'Thread.currentThread().getContextClassLoader().getResourceAsStream()'.Ceramist
@Ranjeet, I think that getClassLoader().getResourceAsStream is the right way to go. May you please edit your answer and elaborate? thanks.Beerbohm
S
1

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

enter image description here

Shakespeare answered 3/2, 2021 at 13:5 Comment(0)
S
0

It's still for me. I tried:

+ "Mark Directory As" ==>"Resources Root"
+ getClassLoader().getResourceAsStream()
+ getClass().getClassLoader().getResource()
and Thread.currentThread().getContextClassLoader().getResourceAsStream()
Shumaker answered 11/8, 2016 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.