For resources in a JAR, you must use ‘URL’ instead of ‘File’.
This snippet works with the IDE (Eclipse) and with a JAR :
URL urlClasses = getClass().getResource("");
String urlText = urlClasses.toString();
String pathImages = null;
if (urlText.startsWith("jar"))
{
pathImages= urlText.replaceAll("abc", "images");
}
else if (urlText.startsWith("file"))
{
int i = urlText.indexOf("classes");
pathImages = urlText.substring(0, i) + "images/";
}
final String IMAGE = "myImage.png";
URL urlImages = null;
try { urlImages = new URL(pathImages + IMAGE); }
catch (MalformedURLException e) { e.printStackTrace(); }
// 'abc' is the name of the root-package,
// 'images' is the name of the resource-directory and is
// placed in the same directory as 'src' and 'classes'.