I want to add a picture to my GUI program created using Eclipse and MyEclipse (for GUI visual design) from the resource pictures I pasted earlier in the project.
I managed to load pictures that lies just beside the .JAR file using
image = ImageIO.read(new File("imageFile.jpg"));
But I want to use the image from my resources "src" folder directly , so that the .JAR file is a standalone file yet loads pictures nicely.
I tried to make it
image = ImageIO.read(new File("src/ldtlogo3.jpg"));
I use this method when exporting the .JAR file
Java: export to an .jar file in eclipse
Use the overloaded ImageIO.read method taking an InputStream as a parameter, and use MyClass.class.getResourceAsStream() to get this input stream. getResourceAsStream loads a resource from the classpath (and thus from the JAR of your application). Its api doc will tell you which path it expects.
Note that the src directory is used to hold your Java source files. The jar doesn't contain it. It contains the .class files, in a hierarchy which directly maps the package hierarchy. Eclipse will automatically "compile" the image file by copying to the output directory, along with the .class files.
Related
im wring a simple program and my images load in fine in the project but as soon as i export it into a jar file none of them load. i have little to no experiance creating artifacts or jar files.
project structure:
project:
src:
myclass1, myclass2...
res:
image1.png image2.png...
im using Toolkit.getdefaultToolkit to load in images
in my class i am loading the images in the constructor by writing
myImage = toolkit.getImage("res/image1.png");
this works perfectly fine in the project. does not work in the jar
i have also tried
myImage = toolKit.getImage("image1.png");
which does not work in the project or while opening the jar
i know toolkit is not the best way to go about loading images but i would like to know how i can fix this issue while using toolkit.
ive even tried using the absolute path to a folder on my desktop and once again they load in fine in my project but they do not get loaded when opening the jar. please help ive tried everything. thanks
(btw) if i open the jar file in intellij the images load but if i open the jar file in finder or from my desktop they do not. i want to be able to send the finished project to someone
The toolkit.createImage(String) method takes in a string, and interprets it as a path, looking for a file at that path. An entry in a jar file is not itself a file. It is therefore impossible to use this method to read image files that are in a jar.
However, that's not the only createImage method. There's also toolkit.createImage(URL) and that is the one you want.
SomeClass.class.getResource("something.png")
This expression works on any class (Foo.class gets you the class instance for Foo, and all class instances have the getResource method), and will look for the named entry in the exact same place SomeClass.class (the file) lives. If SomeClass.class currently lives in a jar file, then that's where it'll look.
Thus, ensure that img.png is in the same place your class file is (ensure it is jarred along with the rest), and that will work. You can also ask for e.g. SomeClass.class.getResource("/foo/bar/img.png"); this will then look from the 'root' of where SomeClass is. So if you have a jar such that if you run jar tvf thatjar.jar and you get:
....
/com/foo/yourpackage/SomeClass.class
....
/foo/bar/img.png
then /foo/bar/img.png works.
thus: Once you know where you put that stuff in your jar file:
toolkit.createImage(YourClass.class.getResource("image1.png"))
is what you're looking for.
I don't quite understand how creation of a Swing application really works.
I have created a simple application that gets a background image from a path.
ImageIcon imageIcon = new ImageIcon("C:\\Java\\ApplicationName\\out\\Resourses\\backGround.jpg");
Image image = imageIcon.getImage(); // Создание картинки из него
Image temp = image.getScaledInstance(500,500,Image.SCALE_SMOOTH);
imageIcon = new ImageIcon(temp);
g2d.drawImage(temp,0,0,null);
It works, so I decided to make a JAR file independent from changes in the Java folder (like deleting this image) and put images in src - images ( package ). I don't know if it is possible though.
I started to get background image like this.
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("/images/backGround.jpg"));
The program works perfectly in IntelliJ IDEA but if I extract it to JAR file I get a blank background screen. Why is that?
And can I have my entire application including images in a single JAR file? One that is not dependent on other folders?
You can certainly put the image and the code in a single jar file. The image is not loading either because it's not in the jar file, or because it's not at the path /images/backGround.jpg inside the jar.
One helpful thing about jar files that you may not know: they are in .zip format, so you can use any zip tool to see what is inside the jar (temporarily renaming the jar file to .zip might make this easier). IF the image is not there, fix your jar build (you didn't say how you are making it, so not sure what you need to change). If the image is there but under a different path, then fix the jar build to put it in the right place, or change the path you are reading it from.
Yes definitely you can include your entire application into single jar file i.e. Runnable jar file. But i would suggest you not to include your image inside the jar file, instead pass that image path to an Property file and use that property file in your code for image path. So that even if next time you need to change the image you just change the path in your property file and you dont have to export your entire project again.
So I have 2 class folders one is res the other is lib. My res folder has two other sub folders one with images the other with sounds. My lib folder has 4 other jar files and a native folder. It all works within eclipse but when I try to export it as a runnable jar it does not work. I won't won't recognize anything.
I am to call my images I am using ImageIO.read(new File(imagePath)); For the sound I am using the external libraries I mentioned earlier to load and play them.
I am to call my images I am using ImageIO.read(new File(imagePath))
Contrary to your title, this is not an Eclipse problem - it's simply a bug in your code, because your code assumes that the image is stored as a file in the file system, when it's not.
You don't have a file for the image, so you shouldn't use new File. You should instead use Class.getResource or ClassLoader.getResource - or the getResourceAsStream equivalents. That way, it will load the resource from whatever context the class itself is loaded, which is appropriate for jar files. So for example, you might have:
Image image = ImageIO.read(MyClass.getResource("foo.png"));
... where foo.png is effectively in the same package structure as the class. Alternatively:
Image image = ImageIO.read(MyClass.getResource("/images/foo/bar.png"));
where images is a folder within the root directory of one of your jar files loaded by the same ClassLoader. (We don't have enough information to give you complete code here, but that should be enough to get you going.)
I want to get the path to a resource for ImageIO to read out a BufferedImage from some .png s.
While developing the project I use a relative path to "/bin/stuff/icons/image.png" , but this will definetly not work when I put everything together into a .jar file, so I need a way to get the path to these resources both while testing in eclipse and when later running it within a .jar .
After a lot of trying out both finding the file and getting the input stream to the file I came to the conclusion that this approach works every time:
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(path)
BufferedImage image = ImageIO.read(in)
Where path is
"projectName/resourceFolder/" + nameOfResource.stuff
as found in the src directory of the eclipse project.
E.g.
"myProject/images/icon.png"
When getting only the resource and then getting the path of the resource to link to a file, you will get FileNotFoundExceptions when using a .jar (but not while testing with eclipse, so one should be warned to think that his code works).
And - no - I don't save images in the bin/ - but they are copied to this directory and thus I find them there while testing. Now everything seems to be working.
Don't put anything under the bin directory in Eclipse: if you run a clean on the project it will be erased.
What you can do is to define a new source folder like resources, and put the image there. This way it will be automatically copied to the bin folder.
If you include the resources folder into the Jar, it will be available in both environments by using something like:
ImageIO.read( getClass().getResource("/image.png") )
PS: You can evade using a different resources folder but mixing the sources and images will quickly pollute your source folder.
I'm exporting a simple java project that includes two directories; src and Icons. Icons is a directory that contains three .png files.
I'm exporting to an executable .jar file using File -> Export. The export works properly and the .jar file contains the Icon directory. But I can't get the correct path for the .png files when the project is deployed. During the development I'm using the following path:
Icons/picture.png
and it works as long as I run from within the Eclipse IDE.
How do I get the correct path for the icons?
Your code is looking for the image outside of the .jar file. Try the URL constructor of ImageIcon instead.
Icon icon = new ImageIcon(getClass().getResource("Icons/picture.png"));
See Class.getResource().
mmyers is correct, but be aware that getClass().getResource() will load resources relative to the package where the class is defined. I suspect your icons are packaged at the root of the jar file and not relative to the class itself. To get resources from the root of the classpath, try:
getClass().getClassLoader().getResourceAsStream("Icons/picture.png")