-1

Hi i am making a quiz application with my project partner and at the end of the quiz i am trying to display an image from out of my resource folders to the winners (a trophy). Everything worked fine untill he tried to run the application on his computer. The project is shared, so if he changes something it changes in my files aswell and vice versa.

But at this time i can't even let it work on my computer anymore.

Here is an image how i placed the image in a resourcefolder called Images. You can see this and the code on here.

My code is as follows :

BufferedImage image = null;
java.net.URL url = getClass().getResource("src/Images/trophy.png"); /* Draw an image for the winning team */
try 
{
    if (url == null)
       System.out.println("TEST");
    else 
    {
        image = ImageIO.read(url);
        g.drawImage(image, w / 2, 300, this); /* Draw the image */
    }
} 
catch (IOException e) 
{
    e.printStackTrace();
}

I've already tried cleaning the project like advised in other posts, i've already changed the location to this aswell "/src/Images/trophy.png" but this isn't working aswell. Anybody knows whats wrong?

Thanks!

Java Enthusiast
  • 654
  • 7
  • 19
user3485470
  • 121
  • 5
  • 11

1 Answers1

0

I am not sure how you are testing your code but it seems like the path you provide for the resource is not in the classpath of the running application.

getResource() uses the class loader mechanism to load resources so the resources must be in the classpath.

Can you tell what is the command you use to run the program (is it from an IDE, e.g: Eclipse)? Do you know how to change the execution classpath (hint)?

  • I use eclipse. And no I don't know how to change the execution claspath. – user3485470 May 28 '14 at 08:42
  • hmm if you use are using eclipse then changing _getClass().getResource("src/Images/trophy.png")_ to _getClass().getResource("/Images/trophy.png")_ should have fixed the issue since eclipse copies any non .java file from the _src_ folder to the _bin_ folder which is in the classpath. Try going to eclipse preferences window (_Window/Preferences_) then in _Java/Compiler/Building_ and make sure "_utput folder/Scrub output folders when cleaning projects_ is checked. Then click the menu _Project/Clean..._ and select your project in the list. – user3680058 May 28 '14 at 08:46
  • It was checked all along. – user3485470 May 28 '14 at 08:56
  • Did you clean the project, build it and launched it again (Check the menu _Project/Build Automatically is checked_)? In the same place in the preference panel there is a field named _Filtered resources_, what does it contain? – user3680058 May 28 '14 at 09:00
  • I can't find the field filtered resources, where is it located? – user3485470 May 28 '14 at 09:58
  • It's located in the same panel as the _Scrub output folders..._ parameter, just below it. What version of eclipse are you using? – user3680058 May 28 '14 at 10:23
  • Well i kinda fixed it now using ImageIcon... – user3485470 May 28 '14 at 12:09