14

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

Community
  • 1
  • 1
Vincent Paing
  • 2,308
  • 3
  • 17
  • 29

4 Answers4

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

Brian Topping
  • 3,235
  • 28
  • 33
  • 1
    Still giving me NoSuchFileException. I marked it as Resource Root. – Vincent Paing Apr 27 '15 at 05: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")```. – Brian Topping Apr 27 '15 at 05:11
  • I have added the code. Can you check it again? Thanks :D – Vincent Paing Apr 27 '15 at 05: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"```. – Brian Topping Apr 27 '15 at 05:13
  • I have tried putting in root directory. It doesn't add to the JAR file also – Vincent Paing Apr 27 '15 at 05:17
  • 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. – Brian Topping Apr 27 '15 at 05:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76322/discussion-between-renges-and-brian-topping). – Vincent Paing Apr 27 '15 at 05:23
  • 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; https://www.jetbrains.com/help/idea/2016.1/configuring-content-roots.html?origin=old_help – Olgun Kaya Apr 30 '16 at 17:43
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.
Ranjeet
  • 636
  • 6
  • 12
  • Still giving me NoSuchFileException after I have rebuilded – Vincent Paing Apr 27 '15 at 05: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 Apr 27 '15 at 05:22
  • @Ranjeet, I think that `getClassLoader().getResourceAsStream` is the right way to go. May you please edit your answer and elaborate? thanks. – 0x90 Sep 30 '15 at 09: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

theapache64
  • 10,926
  • 9
  • 65
  • 108
0

It's still for me. I tried:

+ "Mark Directory As" ==>"Resources Root"
+ getClassLoader().getResourceAsStream()
+ getClass().getClassLoader().getResource()
and Thread.currentThread().getContextClassLoader().getResourceAsStream()
slfan
  • 8,950
  • 115
  • 65
  • 78