Reputation: 604
I have been playing with the idea of using ImageMagic (im4java) to do a comparison of known good page renders against stored good pages.
I have got this working on a test site, but all my images (good, bad and differrent) are stored in my c:\temp folder. I have been toying with the idea of having the "expected" images kept inside the project folder structure, so when the project is checked out, the expected images are there.
(not saying this is a great solution, this is just something I have been playing with.)
So my test is stored in /src/test/java/my.screen.test/compareTest.java and I have my "expected" image in /masterImages/test.png
I have tried various ways to reference this:
I included masterImages in the build path and then tried to use InputStream input = getClass().getResourceAsStream("/masterImages/googleHomePage1.png"); (I then thought I could simply use input.toString() to pass into im4java - but the InputStream gave me nullpointer exception) I also tried removing the masterImages from the buildpath and trying it that way.
I have also tried String path = getClass().getClassLoader().getResource("masterImages/googleHomePage1.png").toString(); Again, null pointer. I know there is something stupid I am not seeing here, like I said this started as me playing but it's now annoying me why I can't get it to work.
Any insights into what I am missing greatly appreciated.
Upvotes: 0
Views: 66
Reputation: 604
Thanks to all. I think I have this sorted. Turns out I had to add the folder containing the image to the classpath - I wrongly assumed that as I had masterImages/otherFolderName that any file inside otherFolderName would be included if I included masterImages. Turns out this is not the case (at least for me.)
Upvotes: 0
Reputation: 419
From - In java, how do you retrieve images from a jar file?
It is indeed simple: you use the various getResource()
methods in java.lang.Class
and java.lang.ClassLoader
. For example, in your app, you could just write
treeURL = getClass().getResource("/images/tree.png");
This would find the file in an images directory at the root of the jar file. The nice thing about the getResource()
methods is that they work whether the files are in a jar or not -- if the images directory is a real directory on disk, this will still work (as long as the parent of images is part of your class path.)
Upvotes: 0