473,657 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Proper way to load files within a jar

5 New Member
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:
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 2346
BigDaddyLH
1,216 Recognized Expert Top Contributor
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
herenbdy
5 New Member
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
herenbdy
5 New Member
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 Recognized Expert Top Contributor
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
herenbdy
5 New Member
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
3649
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 performance reasons, I do not want to store binary files in the db (cuz there will be frequently retrieved and stored and many will be within the range of 10 MB - powerpoint slides and pdf files) so uploaded files will have to stay on the front end web...
0
1364
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 event sink is on an object that can come and go, what is the proper way to deal with adding and removing the handler to the event list? Should the event sink make sure to remove itself from the source's event list before all other references to it...
10
2642
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 happy. Now -- how do I take advantage of that second computer to "load-balance" the web site? Will it really give my users a noticable performance increase? How do you accomplish this? I've read many of those MS articles and it's...
9
3255
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 Tools to do a memory dump of the app when the lock up occurs, however the stack information is not very useful. I have just put a new build of our system onto production, and this build is a "Debug" build as opposed to a "Release" build. I am...
4
1744
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 created a solution that has my four web apps contained in their own projects (correct me if that's not the proper way, but that's an easy way to manage webapps in JBuilder), and I was hoping I could create another project (?) to copy the files over...
4
9595
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 ? How can I share my XSLT ( there are 2 helper stylesheets, and I check that they are also available... the loaded xsl makes reference to the helper xsl files. Where should I start looking ? // Load the stylesheet. errBefore =...
1
3793
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 on our server, there is a breakdown when it tries to load .FLV files sequentially. All the files share the same directory. There are no 'subfolders', all files are in the same root folder called group_quote. Given that the sequential loading...
4
2047
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: 1) I have roughly 700 files that are modified throughout the day by users, within a separate application
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8509
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.