Alex Rivera | Logout

How do I go about adding an image into a java project with eclipse?

Asked 2011-04-14T01:07:33.670
25

I've done a lot of reading around SO and Google links.

I have yet to figure out how to correctly add an image into an eclipse gui project is such a way that the system will recognize find it. I know there's some mumbojumbo about CLASSPATH but it probably shouldn't be this difficult to do.

Let me start by describing what I'm doing...(If someone could correct me, it'd be appreciated.)

Here is my method.

I add the image using the "import wizard" (right click, "import", "general", "file") into an "import directory" I called "/resources"

Eclipse automatically creates a folder called "resources" in the eclipse package explorer's tree view. Right under the entry for "Referenced Libraries".

Note, "resources" isn't under "Referenced Libraries", it's at the same level in the tree.

I then use the following code:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("/resources/image.jpg");
Image logo = ImageIO.read(input);

And at this point, I run the test program and get this error:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at Test.main(Test.java:17)

Thanks for any help in advance!

Edit
Report

1 Answer

0

You can resave the image and literally find the src file of your project and add it to that when you save. For me I had to go to netbeans and found my project and when that comes up it had 3 files src was the last. Don't click on any of them just save your pic there. That should work. Now resizing it may be a different issue and one I'm working on now lol

answered 2012-01-26T22:24:48.283

Your Answer