Java images loaded in ide but not built jar

Multi tool use
Multi tool use


Java images loaded in ide but not built jar



I have my file path as follows:


+------------+ +-----------------+ +------------------+
| | | | | |
| src +-----------> com +--+---> application |
| | | | | | |
+------------+ +-----------------+ | +------------------+
|
| +------------------+
| | |
+---> resources |
| |
+------------------+



My code is in the application folder and my pictures that I want to load are in resources.



Im using the following code to get the images from resources onto a class in application (image is a BufferedImage).


image = ImageIO.read(new File("./src/com/resources/Pad.png"));



This seems to work in my IDE (intellij) as I can see the loaded images as shown



However, when I build, the images aren't being shown. I'm building by:



File -> Project Structure -> Artifacts -> Green plus -> JAR -> From modules with dependencies -> my main class location -> Extract to target Jar



Then I Build Artifact -> Build.



So when I go to the destination of the built jar and run it none of the pictures are being shown



I checked to see if the images were in the built jar by extracting the jar which does contain the pictures but for some reason the code isn't loading the picture.





Please clarify package of ImageIO class. Is it javax.imageio?
– Timur Levadny
Jul 2 at 20:16


ImageIO


javax.imageio





it is import javax.imageio.ImageIO;
– NotSoVisualBasic
Jul 2 at 20:21


import javax.imageio.ImageIO;





Never, ever reference src in your code - for just the reasons you are now having
– MadProgrammer
Jul 2 at 20:26


src




2 Answers
2



You can try to use resourse as a stream instead of file.


image = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("com/resources/Pad.png"))





This has solved it thanks. the stream seems to make it work, do you know why it works as a stream but not when I use a File?
– NotSoVisualBasic
Jul 2 at 20:25





@NotSoVisualBasic Do you understand what a Jar file is? It's just a Zip based file. So when you "Jar" your project, all the files are stored inside a single binary file, this means the "files" can no longer be accessed via the file system (because they don't exist), instead, you need to make use of the API to find and load those resources which are embedded in the Jar file (or, as a neat side affect here, within the class path)
– MadProgrammer
Jul 2 at 20:27





Yeah I understood that a jar file was like a zipped folder but I didn't realise after building you wouldn't be able to access the files like you would if the program was being developed in an IDE. Thanks for the explanation though!
– NotSoVisualBasic
Jul 2 at 20:32



The src path is not available in build jar. You should use class loader to load resources from classpath. E.g. (assuming that 'src' is classpath root)


image = ImageIO.read(new File(this.getClass()
.getResource("com/resources/Pad.png").getPath());



Edited with the @MadProgrammer suggestion.





This didn't work for me I used: image = ImageIO.read(new File(this.getClass().getClassLoader().getResource("com/resources/Pad.png").getPath())); since File wouldn't use URL, and the built jar doesnt have the pics shown.
– NotSoVisualBasic
Jul 2 at 19:53



image = ImageIO.read(new File(this.getClass().getClassLoader().getResource("com/resources/Pad.png").getPath()));





The question is: What is your claspath root?
– piradian
Jul 2 at 20:00





I fixed the sample code by adding .getPath()
– piradian
Jul 2 at 20:03


.getPath()





I don't know, how would I find it?
– NotSoVisualBasic
Jul 2 at 20:04





new File(this.getClass().getClassLoader() .getResource("com/resources/Pad.png") is defunct and pointless. A resource embedded within a Jar file will not be accessible as a File, that's the whole point of using Class#getResource. Also, since ImageIO.read doesn't take a String you're just generating a compiler error - Simple use this.getClass().getResource("...") instead
– MadProgrammer
Jul 2 at 20:24


new File(this.getClass().getClassLoader() .getResource("com/resources/Pad.png")


Jar


File


Class#getResource


ImageIO.read


String


this.getClass().getResource("...")






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ApXj9JR Q3X1,4W9mFD2Ak4WpZqs,iKCx9S,zfOg3uX,HyZ,F qd 8zZwQmx
la17OEfb9,r,Rai rkTX zqdT65,tq,u ECEimG,t6tj Sima6PxlIEUN6Tye3 mpva6od28Blo GesIYrogZs3,Qnk8EHKApC,hVMMedHj

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications