Proper way to load files within a jar

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • herenbdy
    New Member
    • Mar 2008
    • 5

    Proper way to load files within a jar

    I've been working on a Java based game in order to learn Java, but the game is functional only within my IDE (Eclipse).

    This image shows the file structure of my project:

    Jar and HTML file: http://files.herenbdy.com/Game.zip

    All the XML files I use are within the config folder, and all images within library. The applet is inside the main package, Main.java.

    First problem: I found out that Toolkit.getDefa ultToolkit.getI mage(String filename) does not work within a jar, and that I need to get the URL of the file. Trying to use the following code from within the BitmapGraphic class:
    Code:
        private URL getURL(String filename) {
            URL url = null;
            try {
                url = this.getClass().getResource(filename);
            }
            catch (Exception e) { }
    
            return url;
        }
    	public void loadImg(String filename){
    		img = tk.getImage(getURL(filename));
    	}
    The getURL function returns null everytime.
    I write image file names as: "../library/units/Mainchar.png" to pass into the loadImg function. Removing the ".." does nothing. What is the proper way to load images from within a JAR file?

    Second problem:
    Sometimes the java console of my browser displays an error saying that it cannot find the XML file, the filenames I use for xml files is: "../config/maps/test.xml".

    Can anyone help? The game is useless if it can only be run within Eclipse ><
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Considering the paths you pass to getResource(), if you start with dot, for example "./a/b/file.txt", this means that you are starting at the folder holding the .class file in question and then digging into its subfolder a, then subfolder b etc...

    Instead, I think you want to use an "absolute" path becuase of the way you placed your resources:

    [CODE=Java]url = this.getClass() .getResource("/a/b/file.txt");[/CODE]

    This means start at the root of the jar's file structure, find folder a there, then go into subfolder b, etc...

    Comment

    • herenbdy
      New Member
      • Mar 2008
      • 5

      #3
      3rd Edit: Eureka! The JAR functions now, however it required me placing the library and config folders inside the same folder as my source code. However, this looks absolutely ugly in the Eclipse file explorer (as it treats all folders inside the source folder as packages). Is there a way to work around this?

      Comment

      • herenbdy
        New Member
        • Mar 2008
        • 5

        #4
        Nevermind, moving my library and config folders into the "/bin" folder makes the game function both within Eclipse and within a Jar, slightly annoying but it works, thanks for the help!

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by herenbdy
          Nevermind, moving my library and config folders into the "/bin" folder makes the game function both within Eclipse and within a Jar, slightly annoying but it works, thanks for the help!
          That sounds odd. Eventually, you should figure a more logical place!

          Comment

          • herenbdy
            New Member
            • Mar 2008
            • 5

            #6
            Addendum:
            Jar woes once more.

            This time images will not load when the game is run from a Jar, neither in Applet nor Application form (It however works perfectly fine in Eclipse). The entire screen is just black, however the size of the window is correct so clearly some part of the code is working. I figured that the images or xml files were not being loaded, however the Java console does not give me any errors at all (normally if a file was not found, it would give me an Access denied or Null pointer error).

            I decompressed an earlier version of the Jar that worked, and my current version. The folder/file structure is IDENTICAL minus an added class or two, so I really don't understand what's going on... Also I did not make any changes to the way files are loaded in my coding.

            Could anyone help?

            Comment

            Working...