hapciu
asked on
loading resources form a jar file
i want to bundle all my resources in a jar file. how do i make that transparent to all my classes that use resources ? now i have classes that load icons refered by their filenames (such as loadIcon(".res/myicon.gif" )), and i would like them to work the same when i change my resources from a dir to a jar.
Or if you ensure your loadIcon method is overloaded to take a URL as a parameter then you'll also be fine with
loadIcon(getClass().getRes ource("/re s/myicon.g if"));
loadIcon(getClass().getRes
ASKER
what is getClass and whom do i call it upon ??
thanks
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it kinda works. but getResource gives me an url and i can't make a file / filename out of that url (and that's what i need unfortunately). is it pssible to get a file or filename (or an uri) from an url ?
Just use the URL as a filename string because a URL has a .toString() method.
Just check that the URL is not null.
;JOOP!
Just check that the URL is not null.
;JOOP!
ASKER
well, url.toString() or url.getFile() return a string which isn't my file's name, but a full name with protocol, host and everything. if i feed that string to a new File(String name) i get an exception. i guess the only way is to parse that string and extract my filename. but thanks anyways.
I cut this from one of my applications, why should it not work for you?
/**
* Safely get the path to a resource file via Class info.
* <p>If the file is missing, assuming that the resource was an image file,<BR>
* the packaged <b>MISSING.gif</b> icon is substituted,
* using the class info <b>packageClass</b> retrieved elsewhere.</p>
* @param classinfo a Class loaded from the root of relative pathname.
* @param filename the relative pathname to the resource file.
* @return an URL to the resulting resource.
*/
private static URL findResource(Class classInfo, String filename)
{
URL resourceURL;
if(classInfo == null)
{
fatal("BAD CLASS INFO");
}
if((resourceURL = classInfo.getResource(file name)) == null)
{
error("RESOURCE FILE '" + filename + "' MISSING.");
if((resourceURL
= packageClass.getResource(" res/MISSIN G.gif")) == null)
{
fatal("SUBSTITUTE IMAGE 'MISSING' NOT FOUND.");
}
}
return(resourceURL);
}
// And deploying this in my program elsewhere (assume JFrame mainFrame; ):
/**
* Set the default icon on the program main JFrame.
* <p>Can be used to reset the icon after calls with an argument.<BR>
* The default icon should be <b>res/<program>.gif</b>.< /p>
*/
public static void setMainFrameIcon( )
{
mainFrame.setIconImage
(
Toolkit.getDefaultToolkit( ).getImage (findResou rce(this.g etClass( ), "res/Application.gif"))
);
}
;JOOP!
/**
* Safely get the path to a resource file via Class info.
* <p>If the file is missing, assuming that the resource was an image file,<BR>
* the packaged <b>MISSING.gif</b> icon is substituted,
* using the class info <b>packageClass</b> retrieved elsewhere.</p>
* @param classinfo a Class loaded from the root of relative pathname.
* @param filename the relative pathname to the resource file.
* @return an URL to the resulting resource.
*/
private static URL findResource(Class classInfo, String filename)
{
URL resourceURL;
if(classInfo == null)
{
fatal("BAD CLASS INFO");
}
if((resourceURL = classInfo.getResource(file
{
error("RESOURCE FILE '" + filename + "' MISSING.");
if((resourceURL
= packageClass.getResource("
{
fatal("SUBSTITUTE IMAGE 'MISSING' NOT FOUND.");
}
}
return(resourceURL);
}
// And deploying this in my program elsewhere (assume JFrame mainFrame; ):
/**
* Set the default icon on the program main JFrame.
* <p>Can be used to reset the icon after calls with an argument.<BR>
* The default icon should be <b>res/<program>.gif</b>.<
*/
public static void setMainFrameIcon( )
{
mainFrame.setIconImage
(
Toolkit.getDefaultToolkit(
);
}
;JOOP!
ASKER
that should work, i admit. but i need to open an inputStream on that file (which is not an icon, it's a properties file bundled with my resources) - and inputStream NEEDS a file. so i have to get a File from the url your method returns.
InputStream in = getClass().getResourceAsSt ream("/res /myicon.gi f");
Sorry, missed this:
>>it's a properties file bundled with my resources
therefore
Properties props = new Properties();
props.load(getClass().getR esourceAsS tream("/re s/myProps. properties "));
>>it's a properties file bundled with my resources
therefore
Properties props = new Properties();
props.load(getClass().getR
hapciu, next time ask your question right!
You didn't ask for file resources.
Anyway, the last answer by CEHJ is the one that deserves points.
;JOOP!
You didn't ask for file resources.
Anyway, the last answer by CEHJ is the one that deserves points.
;JOOP!
ASKER
ok, so i didn't say the whole thing right from the start... :P
sorry for that and thanks for your help, both of you.
sorry for that and thanks for your help, both of you.
new ImageIcon(getClass().getRe