Load Java Image inside package from a class in a different package
Solution 1
You could either call Class.getResource
and specify a path starting with /
, or ClassLoader.getResource
and not bother with the /
:
URL resource = MyJavaFile.class
.getResource("/PackageB/PackageBa/PackageBaa/MyImage.png");
or:
URL resource = MyJavaFile.class.getClassLoader()
.getResource("PackageB/PackageBa/PackageBaa/MyImage.png");
Basically Class.getResource
will allow you to specify a resource relative to the class, but I don't think it allows you to use ".." etc for directory navigation.
Of course, if you know of a class in the right package, you can just use:
URL resource = SomeClassInPackageBaa.class.getResource("MyImage.png");
(I'm assuming you can pass a URL
to the Image
constructor in question. There's also getResourceAsStream
on both Class
and ClassLoader
.)
Solution 2
you can use relative path since the the relative path is project folder.
ImageIcon img = new ImageIcon("src/PackageB/PackageBa/PackageBaa/MyImage.png");
Solution 3
/folderB/folderBa/folderBaa/MyImage.png
The image can stored into a project folder location .eg: /images/MyImage.png
Then try:
Image img = new Image(/images/MyImage.png);
Using a file path is not possible when running a program that's in a jar file, especially if the program is being loaded as an applet or WebStart application then you can use ClassLoader to get image.
use the following code to load the images:
ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL = cldr.getResource("/PackageB/PackageBa/PackageBaa/MyImage.png");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
Solution 4
This IS the best way to handle all images and icons in a JAR App.
Once you've zipped up all of your images and icons into its own JAR file - Configure your build path by adding the images JAR file into your libraries tab so that its now included in your classpath.
Then simply use the following 3x lines of code at the start of your constuctor to access any image you need for anything including a SystemTray image which doesn't accept the simple ImageIcon's as its main icon (weird I know). The 3x lines are:
URL iconUrl = this.getClass().getResource("/image-iconb.png");
Toolkit tk = this.getToolkit();
imageIcon = tk.getImage(iconUrl);
(imageIcon is just a constructor declared Image variable) Now you can set a window icon as simply as:
setIconImage(imageIcon );
and at the same time use the same variable when setting the System TrayIcon by declaring:
trayIcon = new TrayIcon(imageIcon, "SystemTray Demo", popupMenu);
The above allows you to declare Images or ImageIcons easily and centrally without running the risk of not keeping image resources in the right place. It keeps it nice and tidy, with the JAR containing all your images automatically compiled at run time and distribution of your program.
As a bonus, once the JAR is registered in your classpath - you can keep adding any other images into the same JAR at any time without any fuss too - Everything just works and the added images are instantly available to your app.
Much better in my view.
CodeGuy
Updated on May 06, 2020Comments
-
CodeGuy almost 4 years
I have a Java project called MyProject. I have a few different packages (keeping names simple for the purpose of this question), as follows:
src/PackageA src/PackageA/PackageAa src/PackageA/PackageAa/PackageAaa src/PackageB src/PackageB/PackageBa src/PackageB/PackageBa/PackageBaa
I have a class
src/PackageA/PackageAa/PackageAaa/MyJavaFile.java
And I have an image
src/PackageB/PackageBa/PackageBaa/MyImage.png
Inside of
MyJavaFile.java
, I would like to declare an Image oject ofMyImage.png
Image img = new Image(....what goes here?...)
How can I do this?
-
paulsm4 over 11 yearsI agree - Creating a directory called "/images" is a much smarter, better practice than storing your .png in a source folder/Java package. IMHO...
-
CodeGuy over 11 yearsI need the image, not the image icon. Can I just do a getImage()
-
CodeGuy over 11 yearsthis is incorrect. I get a null pointer. and yes, the path is absolutely correct.
-
CodeGuy over 11 yearsHow can I get a BufferedImage from this Image.
-
Mohammod Hossain over 11 yearswhere is your image location . if it in your package folder then correct. And it is in folder location then ./foldername
-
Mohammod Hossain over 11 yearsAnd it is in folder location then foldername/image.png