Link to home
Start Free TrialLog in
Avatar of ogog
ogog

asked on

Problems accessing resources inside Jar

Hi everyone, I've got a small open source project that i'm having problems with. My development tool is eclipse and it's the tool that compiles the code. Also, I use a .bat file to make the jar file and I think the problem is here. As I've heard of, get resources suits for both directories or jar files. However, even though everything works great on eclipse debugger, when I make the jar, lots of things become unavailable. I'm using helpgui (from sourceforge) and TinyLAF (both open source) to present the help file and use skins. From TinyLAF I corrected the problem on the source code of the project but I can't seem to do it to Helpgui. I know the problem is not even there, but on my jar file. So I deleted the source code of Helpgui from eclipse and referenced the compiled library that exists on their website. However I can't do anything now! Not even in the debbugger. So what I'm asking (and giving all my points to) is that someone patient and willing to make points (I guess easily) will download the project sourcecode into eclipse, take a look at the debbugger and the bat file and tell me what i'm doing wrong. It's the only thing that ain't fixed. This little bug is preventing me from releasing the binaries as jar executables.

Thank you all in advance.

Eclipse project folder: http://cannonbang.sourceforge.net/cannonbang.rar

The line that triggers help gui is on the class: net.sourceforge.cannonbang.GuiMain (as is the main method) I understand that it takes time and that's why i'm giving big points.
Avatar of sciuriware
sciuriware

How to access a resource (file) inside your .jar file:

If the file is in a directory 'res' that is in the directory of a certain .class file, then:


   /**
    * 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.
    */
   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!
Avatar of ogog

ASKER

I do know that. I can get images from the jar file usually. I really do! I've always done this. With this project i'm finding that impossible so i think i'm doinng something wrong. Not even the icon on the JFrame i can get from within the jar. I usually did this. That's why this time i'm asking for someone to take a look at the project and a look a the way i'm generating the jar. it seams to forget where everything is!
First check that your .jar is OK:
rename it to .zip, then unzip it in a free directory.
The directory structure that you will get that way is the structure that is 'seen' by the above code.
So: is every file in the right directory.

If not try to rebuild the .jar using the command-level jar.exe

;JOOP!
Avatar of ogog

ASKER

that's what i do. i open it with winrar to check it. and it is ok.
i have built it with the jar.exe wich is in the bat as i've said before.

it's done just like anyother that i do and that works! this one is... "special"
ASKER CERTIFIED SOLUTION
Avatar of ogog
ogog

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No thanks, congrats!

;JOOP!