Friday, July 5, 2013

URI is not hierarchical - Java

There are some problems when running the build jar of your project compare to running it in an IDE, when resources such as images is called within the build jar, usually we just use getClass#getResource("path/of/the/image.jpg") in the IDE, and when you build the project and execute the jar the "URI is not hierarchical" is thrown. One way to fix this is retrieve it as a resource-stream like.

InputStream dx = this.getClass().getResourceAsStream("/com/sample/images/default_image.png");
Image sm = ImageIO.read(dx).getScaledInstance(width, height, Image.SCALE_SMOOTH);

You can then us the Image in any component.