15

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 of MyImage.png

Image img = new Image(....what goes here?...)

How can I do this?

CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • 1
    Q: Why are you putting a .png resource in a Java package??? Even if you put stuff in a .jar file, you'll usually have a separate directory for resources ("/resources" being a Pop Favorite). For example: [Add image to Jar](http://stackoverflow.com/questions/1096398/add-image-to-jar-java). PS: This link answers your specific question ... but I would advise against it, if possible. IMHO... – paulsm4 Aug 28 '12 at 06:08
  • src/PackageB/PackageBa/PackageBaa/MyImage.png package will be folder – Mohammod Hossain Aug 28 '12 at 06:10
  • Image image = Toolkit.getDefaultToolkit().getImage("Agent.gif"); – TechDog Dec 22 '16 at 04:57

6 Answers6

20

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

you can use relative path since the the relative path is project folder.

 ImageIcon img = new ImageIcon("src/PackageB/PackageBa/PackageBaa/MyImage.png");
mohamed abdallah
  • 134
  • 2
  • 10
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);
Oli
  • 9,766
  • 5
  • 25
  • 46
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
1

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.

Martin Sansone - MiOEE
  • 4,281
  • 1
  • 29
  • 31
0

Use the getResource method to read resources inside the src root. For example, the following code retrieves images from a folder src/images.

// Get current classloader
ClassLoader cl = this.getClass().getClassLoader();
// Create icons
Icon saveIcon  = new ImageIcon(cl.getResource("images/save.gif"));
Icon cutIcon   = new ImageIcon(cl.getResource("images/cut.gif"));

The example assumes that the following entries exist in the application's JAR file:

images/save.gif
images/cut.gif
Lazy Coder
  • 307
  • 4
  • 12
0
Image img = new Image("./src/PackageB/PackageBa/PackageBaa/MyImage.png");

This shall go the path of the image is first inside src (source) then package so the program would access the image this way.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36