0

I'm working on a Java program using Eclipse. Right now, I have an src folder that contains 2 packages: memory.views and memory.resources.

In the memory.views package, I have my Main.java file. In the memory.resources package, I have my .txt file and .gif file.

Within the program, I have no problem accessing (and manipulating) the .txt file by using the path /memory/resources/name.txt. However, when I do the same with the .gif file using the code below, I get no result:

ImageIcon icon = new ImageIcon("/memory/resources/name.gif");

There's no error produced. The only effect is that I see no image when the program is running.

I've tried also writing the following, but none worked:

ImageIcon icon = new ImageIcon("/resources/name.gif"); <br>
ImageIcon icon = new ImageIcon("name.gif");

Now, just so nobody says that it's the .gif file's fault, I've actually entered in the full Finder path (I'm using a Mac) and that worked perfectly:

ImageIcon icon = new ImageIcon("/Users/[...]/src/memory/resources/name.gif");

However, I don't want to do the full path, because if I export the program and run it on another computer, then the non-programming computer won't display the image either.

So, right now, I don't even know what the problem is. The .gif file works sometimes, but not when it's accessed via the same path as the .txt file, which works all the time. I tried looking here (Cannot access resource by using the getResources() method), but it seems like he had a slightly different problem from me.

Community
  • 1
  • 1
user2323030
  • 1,193
  • 4
  • 16
  • 37
  • I've recently just found an answer to this question (partially thanks to something I read here: http://stackoverflow.com/questions/12436851/icon-in-src-folder?rq=1). What I did was 'ImageIcon icon = new ImageIcon(Main.class.getResource("/memory/resources/name.gif"));'. :) Now the only thing left to do is to test it on another computer. Thanks for everyone's help! I appreciate it so much. – user2323030 Apr 27 '13 at 09:20

3 Answers3

0

You can use

URL url = ClassLoader.getSystemResource("name.gif");
ImageIcon icon = new ImageIcon(url);

provided that your name.gif file ends up in classpath after compilation/build.

vitaly
  • 2,755
  • 4
  • 23
  • 35
  • Thank you, vitaly. I tried this, but it also didn't work. It gave me the a java.lang.NullPointerException error. – user2323030 Apr 27 '13 at 09:12
  • Ok, so I guess your classpath was pointing to the root / and your gif could not be found because it was in "/memory/resources/" subdirectory. `ClassLoader.getSystemResource("/memory/resources/name.gif");` should have worked – vitaly Apr 30 '13 at 20:04
0

What is this ImageIcon class? Is it your own code?

Try Thread.currentThread().getContextClassLoader().getResourceAsStream("/memory/resources/name.gif");

Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71
  • Thank you, Bhushan Bhangale. ImageIcon is not my own class, I found an example of it somewhere (I forget now). In regards to your code, I'm having a bit of trouble understanding it – mainly due to a lack of context. I'm not sure where I'm supposed to insert that. – user2323030 Apr 27 '13 at 09:11
0

See if this works...

File file = new File("/memory/resources/name.gif");
ImageIcon icon = new ImageIcon(file.getAbsolutePath());

threadfin
  • 116
  • 3
  • Thank you, deviprasad742. I tried this, but it didn't work. It did not return an error though, there was just no image displayed. – user2323030 Apr 27 '13 at 09:09