jars and file reading / writing

hi,

i just wondered about this topic lately…
when making any sort of game, it is usual to package it as a jar (and often use it via web start). And once you do it, how can you for example prepare a directory for loading and saving games? or a directory of maps where custom ones can be added later?

The general pattern has become using the users home directory (System.getProperty(“user.home”)) and creating a directory to store stuff in for instance “.MyGameHere”.

Kev

well, there is some annoying detail: it won’t be properly deleted when deinstalled (via JWS).

Couldn’t we directly get the destination repertory of where JWS stores the jars?

loading and saving

I use “muffins” :slight_smile:

They are like cookies but for webstart. Well, they are somewhat odd to handle and there aren’t many examples on the net (the most useful site was actually written in korean ::)).

One of the annoying pitfalls is that you can’t overwrite a muffin, which leads to a somewhat odd try/catch structure. Eg you need to delete it each time, but there won’t be one on the first run.

I also added a nice method called showURL… it starts the client’s default browser with the specified url as parameter.


import javax.jnlp.*; //you need the webstart toolkit
import java.net.*;
import java.io.*;
[...]
      public boolean showURL(URL url) //this one is pretty nice, too
      {
            try
            {
                  BasicService bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
                  return bs.showDocument(url);
            }
            catch(UnavailableServiceException ue)
            {
                  return false;
            }
      }

      public void writeConfig()
      {
            PersistenceService ps;
            BasicService bs;
            URL configURL;
            try
            {
                  ps = (PersistenceService)ServiceManager.lookup("javax.jnlp.PersistenceService");
                  bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
                  URL baseURL = bs.getCodeBase();
                  System.out.println("CodeBase was " + baseURL);
                  configURL = new URL(baseURL, "config");
            }
            catch(Exception e)
            {
                  System.out.println("[E]failed to save configuration");
                  return;
            }
            try
            {
                  ps.delete(configURL);
            }
            catch(Exception e)
            {
                  System.out.println("there wasn't a muffin - first save");
            }
            try
            {
                  ps.create(configURL, 1024); //1024 bytes for our data

                  FileContents fc = ps.get(configURL);
                  DataOutputStream os = new DataOutputStream(fc.getOutputStream(false));
                  os.write[...whatever...]
                  os.flush();
                  os.close();
                  System.out.println("saved configuration");
            }
            catch(Exception e)
            {
                  System.out.println("[L]failed to save configuration");
                  e.printStackTrace();
            }
      }

      public void readConfig()
      {
            try
            {
                  PersistenceService ps = (PersistenceService)ServiceManager.lookup("javax.jnlp.PersistenceService");
                  BasicService bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
                  URL baseURL = bs.getCodeBase();
                  URL configURL = new URL(baseURL, "config");
                  FileContents fc = ps.get(configURL);
                  DataInputStream in = new DataInputStream(fc.getInputStream());

                  in.read[...whatever...];
                  
                  in.close();
            }
            catch (Exception e)
            {
                  System.out.println("unable to load settings");
            }
      }
[...]

The muffin location on win9x is here:
<userdata>\Sun\Java\Deployment\javaws\cache\muffins
(and my program seems to be the only one which uses muffins)

Well, you can’t save as much data as you like to. There are some limits and you can gently ask for some more room… haven’t grasped the whole concept since the documentation is somewhat fuzzy there.

However, the good thing is that you won’t need any permissions for the stuff above.

I forgot to mention that the stuff above only works with programs startet through webstart!

So… get a server for your lan (for less pain ;)). I recommand xampp. It’s a nice package with apache, php, mysql and what’s not. The setup is a piece of a cake (I mean it). Oh and it’s free :slight_smile:

http://www.apachefriends.org/en/xampp.html

Oh and that [E] and [L] thingies in the source above just mean “early” and “late”.

Indeed. And I’m convinced that when webstart was announced, this functionality was on the features list - per-application data that was managed by webstart. In the end, that seemed to turn out just to be the prefs API (which doesn’t quite do that), but I’m sure I saw this feature clearly described in Sun marketing docs :(.

Or am I dreaming?

Or is there such a thing and I just haven’t found it yet? :wink:

You posted whilst I was responding :).

[quote]> loading and saving

I use “muffins” :slight_smile:
[/quote]
Is this, then, what I was talking about? The official webstart-encapsulated way of storing config etc? Cool.

The problem is…javax.jnlp isn’t included in the JDK (docs). This seems strange, since I would have thought that the packaging system for the main way of distributing apps would be considered a core part of the JDK!

Ah, yes, that was another one of the features touted…

again i’m very impressed onyx! bravo!
…but in the same time, it looks extraordinary complex! For one config file, ok, but i guess listing saved games and other game data would be a lot of work, isn’t it? :-/

btw, i didn’t understood what you meant with your lan stuff in the second post. ???

The problem is…javax.jnlp isn’t included in the JDK (docs).

Yea, well you need that webstart toolkit

This seems strange, since I would have thought that the
packaging system for the main way of distributing apps
would be considered a core part of the JDK!

No, it’s not strange.

It’s silly. The jnlp jar needed for compilation is… behold… a whopping 7 kilo bytes in size ::slight_smile:

Well, that toolkit thingy is somewhere over at suns page (oh oh). Stumbled upon it somewhat accidently. Actively searched for it 2 weeks ago and was unable to find it… aw… I hate the structure of sun’s pages.

Btw that jnlp.jar is only needed for compilation. You don’t need to include it into your distribution (because webstart has that classes… uhm… ::))

@misterX

but in the same time, it looks extraordinary complex!

Indeed it is. I guess it makes sense to wrap up that stuff a bit, because it usually doesn’t matter where it fails - just if.

[quote][…]
btw, i didn’t understood what you meant with your lan stuff in the second post. ???
[/quote]
Ah… uhm…

Well, if you want to start it through webstart it (jnlp+jar(s)) needs to come to your machine through the http protocol. That only works if you have a server and/or webspace somewhere.

However, uploading all the time makes testing really tiresome, therefore it’s much better if you have a local webserver either within your lan (=local area network) or on your own machine. Testing is much nicer with xampp (or apache for that matter).

Eg http://onyx.bubblegumcrises.jp/tinyrivers/ is infact just a directory on my harddrive ;D it’s of course only accessable within my lan and not from the outside (the www)

Btw I picked that domain name, because it can’t exist. If it would exist, I won’t be able to acess it.

That domain stuff is part of the network configuration. “bubblegumcrises.jp” is the domain and “onyx” is the name of my machine (uh uh how creative).

Well, if you don’t need fancy names (hehe) you can aswell just type http:///subdir or http://localhost/subdir

ah, oh, thanks

Two muffin utility classes over there:
http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=share;action=display;num=1091875312;start=0#0