divitum
divitum

Reputation: 11

How to include .png files in Quarkus build

I have .png files in src/main/resources that I want to access on my deployed Quarkus app, but when I use Thread.currentThread().getContextClassLoader().getResource("image.png") it points me to the jar file.

io.vertx.core.file.FileSystemException: Unable to open file at path '/file:/deployments/app/app-api-1.0.0-SNAPSHOT.jar!/image.png'

However, when I use Thread.currentThread().getContextClassLoader().getResource("image.png") while running the app in my IDE, the ContextClassLoader points to the target/classes folder.

I have tried including them in META-INF/resources. I have tried quarkus.native.resources.includes=*.png

Is there a solution that will work for both locally in my IDE as well as deployed on a container/in a native image?

Upvotes: 1

Views: 82

Answers (2)

divitum
divitum

Reputation: 11

For anyone facing a similar problem in the future, what ended up working for me was using the .getResourceAsStream() method instead of .getResource(). I am not knowledgeable about how these things operate, but it looks like .getResourceAsStream() makes a 'connection' to the JAR while .getResource() doesn't.

Upvotes: 0

Tea Curran
Tea Curran

Reputation: 2983

META-INF/resources is the directory Quarkus uses for it's web server, but the java standard is everything in resources get's packaged in the jar's class path.

so if your file is here: src/main/resources/META-INF/resources

The proper way to reference it is:

Thread.currentThread().getContextClassLoader().getResource("META-INF/resources/image.png");

When loading the file with getResource, you can put the file anywhere in your resources directory, like: src/main/resources/image.png.

It will then be available with:

Thread.currentThread().getContextClassLoader().getResource("image.png");

Upvotes: 1

Related Questions