Solved

Problems accessing resources inside Jar

Posted on 2007-11-15
6
1,479 Views
Last Modified: 2013-12-15
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.
0
Comment
Question by:ogog
  • 3
  • 3
6 Comments
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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!
0
 
LVL 1

Author Comment

by:ogog
Comment Utility
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!
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:ogog
Comment Utility
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"
0
 
LVL 1

Accepted Solution

by:
ogog earned 0 total points
Comment Utility
You are not going to believe this! I found the problem! The path of my Project folder in eclipse is: /eclipse/Cannon Bang!/

Anyone that would have looked at the bat would have found the problem. (Actually, anyone that would remember this little tiny detail)

The diference between the normal path and the jar path is that Java adds a "!" near the jar file. So that it knows that from there on it is inside the jar file. It uses the jar with an exclamation point as a directory to know it is not a directory but the jar file!

Here's how I solved the problem: Renamed the "Cannon Bang!" directory to "Cannon Bang". (NO EXCLAMATION POINT)

I'm posting this here in case someone has the same problem.
Thanks sciuriware for trying to help.
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
No thanks, congrats!

;JOOP!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

771 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

10 Experts available now in Live!

Get 1:1 Help Now