Intellij resources not in built artifact
Asked Answered
H

2

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.

Hervey answered 5/2, 2015 at 6:19 Comment(0)
U
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.

Utu answered 5/2, 2015 at 8:6 Comment(4)
Its kind of lame from IntelliJ why resources are not considered as compile ouput.Adaurd
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.Evonevonne
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.Oler
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.Kareykari
F
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.

Freaky answered 5/2, 2015 at 6:53 Comment(2)
This is something I have tried. It doesn't appear to be the issue.Hervey
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 directlyFreaky

© 2022 - 2024 — McMap. All rights reserved.