I have a eclipse web project with src and webcontenet folders. I have a java file in test.xxx.pm.util -> Test.java in which I have the following code:
String url = "images/icon1.gif";
ImageIcon icon = new ImageIcon(Test.class.getResource(url),"Test icon");
The file icon.gif is under Webcontent/images directory.
this gives runtime exception: cannot locate file [gif file]. Why ? Where should I put the images for the jva code to access ? I prefer putting in this webcontent/images directory. Thanks all
public URL getResource(String name)
. . .
This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResource.
Your images must be on the classpath to be found with Class.getResource. Either change the image location or add the directory to the classpath.
I even tried using ImageIcon icon = new ImageIcon(url,"Test icon"); where url = ""; //for this if I put the complete path it works fine. [like C: etc]
But this file/app has to be deployed as a webapp and installed on server. Thats why I tried putting in webcontent folder and hence accessing it..
Any ideas ?