Solved

Problems accessing resources inside Jar

Posted on 2007-11-15
6
1,490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 20288639
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
ID: 20288812
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
ID: 20288926
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ogog
ID: 20290516
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
ID: 20303691
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
ID: 20304726
No thanks, congrats!

;JOOP!
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 53
throw exception 21 69
Export all vba modules from outlook vbaproject.otm 7 50
How to log java errors in tomcat 8 24
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …

730 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