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

4 Answers 4

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

8
  • 1
    Still giving me NoSuchFileException. I marked it as Resource Root. Commented Apr 27, 2015 at 5:09
  • 3
    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"). Commented Apr 27, 2015 at 5:11
  • I have added the code. Can you check it again? Thanks :D Commented Apr 27, 2015 at 5:13
  • 1
    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". Commented Apr 27, 2015 at 5:13
  • I have tried putting in root directory. It doesn't add to the JAR file also Commented Apr 27, 2015 at 5:17
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.
3
  • Still giving me NoSuchFileException after I have rebuilded Commented Apr 27, 2015 at 5:10
  • @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()'.
    – Ranjeet
    Commented Apr 27, 2015 at 5:22
  • @Ranjeet, I think that getClassLoader().getResourceAsStream is the right way to go. May you please edit your answer and elaborate? thanks.
    – 0x90
    Commented Sep 30, 2015 at 9:13
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

0

It's still for me. I tried:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.