From c64339c9451c8297ae8a8ea591090f74f90baa60 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Fri, 2 Dec 2022 12:04:51 +0100 Subject: [PATCH] fixed image loading when app is packed as jar https://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream --- .../gartenverwaltung/json/PlantImageDeserializer.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java b/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java index 29e055a..f0b818b 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java @@ -21,13 +21,9 @@ public class PlantImageDeserializer extends JsonDeserializer { @Override public Image deserialize(JsonParser parser, DeserializationContext context) throws IOException { Image result = null; - URL imageUrl = PlantList.class.getResource(String.format("images/%s", parser.getText())); - if (imageUrl != null) { - try (InputStream is = new FileInputStream(new File(imageUrl.toURI()))) { - result = new Image(is); - } catch (IllegalArgumentException | URISyntaxException e) { - throw new IOException(String.format("Cannot find Image \"%s\"\n", imageUrl.getFile())); - } + InputStream is = PlantList.class.getResourceAsStream(String.format("images/%s", parser.getText())); + if (is != null) { + result = new Image(is); } return result; }