How to access image file in Eclipse project folder

I have one image file in my Eclipse project folder where the image file (logo.jpg) is stored. However, I don't know how to access it in my main program.

I have tried the following

private static URL logoPath

public class MainApplication
     public void createGui()


The problem is I still get a null pointer exception, so obviously the path is not being executed correctly, otherwise logoPath.getClass () disables it.

Any ideas?


source to share

3 answers

You need to place your resources in your java source directory for them to be visible. You have two options:

  • Move the Resources folder to the existing src folder.

  • Create a new source folder for resources only. You can do this in the Java Build Path property page of the project properties.

A few things to observe ...

  • Pay attention to your capitalization. Finding Java resources is case sensitive.

  • The path you will be using will be relative to the source folder (not the project root). For example, if you create a resource folder in the source folder, your path should be "images / ...". If you want to store the resource folder in the search path, you need to create an additional folder level in your project to serve as the original root for the resources.

  • I'm not sure if this is the real problem, but resource paths should not start with a leading slash. They are not paths in the traditional sense. Think of them as class names corresponding to packages, but instead of '/' instead of '.' as a separator.



U can use this way I have the following package structure

  • src / test (test package contains Java files)

  • src / images (images in the folder contain images)

I'm going to get an image from src / images / login.png to src / test / *. java

JLabel label = new JLabel(new ImageIcon(getClass().getResource("/images/login.png")));



I do it the same way

 String filename = "logo.jpg";



And the file structure looks like this:



My problem was before I called "resources" direcototry and it should be "resource"



All Articles