473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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.getDefaultToolkit.getImage(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:
Expand|Select|Wrap|Line Numbers
  1.     private URL getURL(String filename) {
  2.         URL url = null;
  3.         try {
  4.             url = this.getClass().getResource(filename);
  5.         }
  6.         catch (Exception e) { }
  7.  
  8.         return url;
  9.     }
  10.     public void loadImg(String filename){
  11.         img = tk.getImage(getURL(filename));
  12.     }
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 ><
Mar 16 '08 #1
5 2334
BigDaddyLH
1,216 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1. url = this.getClass().getResource("/a/b/file.txt");
This means start at the root of the jar's file structure, find folder a there, then go into subfolder b, etc...
Mar 16 '08 #2
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?
Mar 16 '08 #3
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!
Mar 16 '08 #4
BigDaddyLH
1,216 Expert 1GB
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!
Mar 17 '08 #5
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?
Mar 20 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Samuel | last post by:
I am building an web application that will be hosted on load balanced servers (multiple front and one backen db). The application will have to allow users to upload files onto the server. For...
0
by: BlueMonkMN | last post by:
I've been trying to think of the right way to design relationships between objects with different desired lifetimes that raise events. If an event source is a relatively permanent object and the...
10
by: GeekBoy | last post by:
Okay, I have two identical web servers running Windows 2003 web server. I have an ASP.NET application which runs great on one of them. Dedicated IP address, behind our firewall, etc. Everyone's...
9
by: Microsoft News Server | last post by:
Hi, I am currently having a problem with random, intermittent lock ups in my ASP.net application on our production server (99% CPU usage by 3 threads, indefinately). I currently use IIS Debug...
4
by: Flip | last post by:
What is the proper/suggested/best way to deploy a webapp to a Win2K3 Server running IIS6? I am coming from the j2ee world (easy on the flames please, I'm moving to ..NET remember :>), and I've...
4
by: andrewcw | last post by:
I am moving some code forward from .NET 1.1. I was able to load the XSL file and perform the transform. The MSDN documentation looks like it should be easy. But I get a compile error. Ideas ?...
1
by: redet | last post by:
A demo created by a person who has since left, runs fine on a local (Windows XP) system within flash player 8, but when we try to access it through the html version (index.html) on a local system or...
4
by: Chris Nethery | last post by:
Hello everyone, I have a challenging issue I need to overcome and was hoping I might gain some insights from this group. I am trying to speed up the process I am using, which is as follows: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.