Maybe somebody can help me with a quick question

I have been working on a spare-time project for a while, and I work on it so intermittently that I feel uncomfortable with some areas of the code.

For example, I have a decent working game right now, but I’m not sure how exactly to package it up into a webstart or anything of the sort.

But that’s not my big concern right now. My biggest problem is that my game loads .txt (I can probably switch that to xml) files describing how to draw certain units in my game. I want my game to be fairly editable when it is completed, so I would like to give people the option of creating their own files to describe the shape of various game objects.

Looking at my code, I see that I have a problem:
public linemap(String filename){
String newfilename = “C:\Documents and Settings\Dan\Desktop\race\design\” + filename;

I’m pretty sure that most people will not have the same path as me for their files. Is there a way to define a general path for saving these things? Is there a way to have the program just look for a text file within its own path?

I know I sound very clueless about this, and that’s because I happen to have huge holes in my knowledge of java despite my natural affinity for logic.

If somebody has an answer to this, I would really appreciate it. If there is a tutorial somewhere on the net that I obviously need to read about this, that would be great too. This is one of the obstacles between me and my goal of finally completing this game that has consumed me.

Don’t use files, use URLs instead and load the standard resources from the classpath (e.g. from inside your games jar). For user supplied files either provide them with a filechooser or use a subdirectory for your game under FileSystemView.getFileSystemView().getDefaultDirectory(), which resolves to “My Documents” for windows, the “Documents” directory for MacOS X (I heard) and “/home//” for other unix like OSes.

The location of your main jar can be found with


File jarFile = new File(<YourMain>.class.getProtectionDomain().getCodeSource().getLocation().toURI());
File appDir = jarFile.getParentFile();

as I learned recently (didn’t try it for myself, yet).

Thanks for your help. I see that my next step is to learn about jars.

There’s no reason not to use files if that suits you. Most games will have a visible “Data” directory that contains a bunch of images and sounds and the like, maybe compressed and maybe not. In that case, I usually just use:

new File(System.getProperty(“user.dir”) + “/Data/Images/Monkey.png”) or whatever the path would be.

Then it’s much easier for your users to get at the files or make their own. This can obviously be a good or bad thing, depending upon what you’re looking for.

Keeping stuff in a TXT is a lot less awful than most people make it out to be. XML can be useful but depending upon complexity it can also be worthless. My simpler games usually have level files laid out like this:

ParamName1 param0 param1 param2 param3
ParamName2 param0 param1 param2 param3
ParamName3 param0 param1 param2 param3
Tiles AAABBAABBAA AAABBAAABAA AAABBAABBAA AAABBAABBAA

ParamName can be whatever you want, like “EnemyTypes” or something and then the following items can be the actual values you use. To get at this, you just use a BufferedReader and String.split(), then you know that the 0th entry is the type of parameter, then the ones following are the parameters for it. Easy.

XML, when included with a parser (which Java has already) can be nice because it’s a lot better laid out and can represent anything, but it also has a lot of wasted characters and can be more difficult to read.

You can also go to the next level and use Serializable and ObjectOutputStream, which works great for most complicated purposes, although I usually end up with like a WritableObject that knows how to properly construct the saved objects, instead of directly writing the saved objects… so it sort of has the same type of functionality as XML in the end.

Keep in mind though that “user.dir” points to the current working directory. This could be anything depending how the user starts the app, but in 98% it will indeed be the application installation directory…

I’d second this. XML is way way overused for stuff like this. XML is great when you need data interchange, it is NOT a good solution when you need simple config files.

Hm, do you know of a more accurate way to get the game’s dir? But I admit you’re right about this. Those 2% situations are going to be weird situations, though.

I too would really like to know how to do this. I feel disgraceful referencing something that is obviously specific to my own computer.

On top of that, I’ve been working on this game for 3 years now (at a rate of about 1 hr/month) and I would love to be able to just send somebody a zip file or a link to a download server so they can see what I’ve got going.

I figure I would use webstart because people here seem to get really excited about it, but I’m sort of walking blind at this point.

Thanks for all your posts, guys. I think I’m getting a better and better idea of how to approach this.

Revisit my first post in this thread. :wink:


File jarFile = new File(<YourMain>.class.getProtectionDomain().getCodeSource().getLocation().toURI());
File appDir = jarFile.getParentFile();

I just tested it and it works fine for locally executable jars. It will naturally fail for webstart though. In this case making yourself a dedicated game-settings directory under FileSystemView.getFileSystemView().getDefaultDirectory() will probably be best practice.

Oh, I thought you were saying those only worked for browsing inside the classpath (i.e. inside a JAR), not for File browsing as well.