Solved

loading resources form a jar file

Posted on 2003-11-17
13
434 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:hapciu
  • 5
  • 5
  • 3
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9767570
You can do

new ImageIcon(getClass().getResource("/res/myicon.gif"));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9767575
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().getResource("/res/myicon.gif"));
0
 

Author Comment

by:hapciu
ID: 9767676
what is getClass and whom do i call it upon ??
thanks
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
ID: 9767694
getClass() is called on the this reference so you should be fine unless there is no 'this' in scope.
0
 

Author Comment

by:hapciu
ID: 9767830
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  ?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9769551
Just use the URL as a filename string because a URL has a .toString() method.
Just check that the URL is not null.
;JOOP!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:hapciu
ID: 9770450
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.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9770512
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(filename)) == null)
      {
         error("RESOURCE FILE '" + filename + "' MISSING.");
         if((resourceURL
            = packageClass.getResource("res/MISSING.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(findResource(this.getClass( ), "res/Application.gif"))
      );
   }


;JOOP!
0
 

Author Comment

by:hapciu
ID: 9770540
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9772228
InputStream in = getClass().getResourceAsStream("/res/myicon.gif");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9772240
Sorry, missed this:

>>it's a properties file bundled with my resources

therefore

Properties props = new Properties();
props.load(getClass().getResourceAsStream("/res/myProps.properties"));
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9776697
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!
0
 

Author Comment

by:hapciu
ID: 9781085
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
get weblogic logged in user in java 2 57
Error with Java/Cache JDBC Classpath 2 30
windows explorer path to command prompt 5 30
servlet example issue 6 29
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now