Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij resources not in built artifact

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.

like image 994
Beryllium Avatar asked Feb 05 '15 06:02

Beryllium


People also ask

How do I enable build artifacts in IntelliJ?

From the main menu, select File | Project Structure ( Ctrl+Alt+Shift+S ) and click Artifacts. and select an artifact format. On the page that opens in the right-hand part of the dialog, specify the artifact settings and contents.

How do I add a resources folder to my Java project in IntelliJ?

From the main menu, select File | Project Structure Ctrl+Alt+Shift+S and click Project Settings | Modules. Select the necessary module and then open the Sources tab in the right-hand part of the dialog. Click Add Content Root and specify the folder that you want to add as a new content root. ).

What are build artifacts?

Build artifacts are files produced by a build. Typically, these include distribution packages, WAR files, reports, log files, and so on. When creating a build configuration, you specify the paths to the artifacts of your build on the Configuring General Settings page.


2 Answers

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.

like image 144
Wagner Michael Avatar answered Oct 09 '22 00:10

Wagner Michael


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.

like image 1
MxR Avatar answered Oct 09 '22 00:10

MxR