14

I'm attempting to build a .jar that contains a non-standard images and fonts, which I have added to the resource folder in my project. I am able to load them into my project with the following code.

URL url = ClassLoader.getSystemResource("./some_font.ttf");

Font font = Font.createFont(Font.TRUETYPE_FONT, url.openStream());

While this works when I run the program in the IDE, when the exported jar is run it is unable to load the font.

Beryllium
  • 556
  • 1
  • 7
  • 20

2 Answers2

18

You can create your own artifacts in the project structure: enter image description here

There you can add the directory contents of the resources folder. Press the green plus and navigate to your folder and add it. You can then click build-> build artifacts -> build. In my case the jar is created in ./out/artifacts folder.

Wagner Michael
  • 2,172
  • 1
  • 15
  • 29
  • 5
    Its kind of lame from IntelliJ why resources are not considered as compile ouput. – igr Dec 04 '15 at 11:30
  • 2
    At least it's not clearly explained anywhere in the IntelliJ documentation how it makes the decision as to whether to include resources in the jar or not. – Carlos A. Ibarra May 22 '19 at 07:44
  • 1
    Well, it gives you the freedom to explicitly define what you want to include in your artifact. However, I would recommend to use a build tool to define how to build artifacts. Gradle, for example, automatically includes the resources folder in the "jar" task. – Omnibyte May 22 '19 at 08:42
  • Omnibyte, for me this problem happens when i do use Gradle. But neverveless i can not use "Build and run = Gradle" in Intellij because in this case forms initialize wrong (this issue has solved only by setting "Build and run = IntellijIdea"). But with "Build and run = IntellijIdea" IDE does not include automaticly Resources that places not in "src" folder. Vicious circle. – Dmitriy May 29 '20 at 08:32
1

Intellij is treating folders marked as "resources" as additional classpath directories which could be discovered by it's own autocomplete or in your case run/debug feature. Try right click on this folder and choose "Mark Directory As">"Unmark as Resources Root" to verify this assumption. If it's stopped working, you need to provide more reliable path taking into consideration where this file is located inside your resulting jar-file.

MxR
  • 586
  • 6
  • 15
  • This is something I have tried. It doesn't appear to be the issue. – Beryllium Feb 05 '15 at 07:10
  • It's hard to suggest what could be the problem without seeing actual project, but assuming most basic project structure consisting of 2 properly marked folders /src and /resources and default jar creation from "Modules with dependencies..." solution could be to refer to resource as URL url = ClassLoader.getSystemResource("some_font.ttf"); because it should be placed at the root level inside the jar and could be addressed directly – MxR Feb 10 '15 at 05:19