Only one JAR file and a savegame feature. Posible?

That’s what I’ve been asking myself since I read the criteria for selecting featured games. I’m talking about this, specifically:

[quote]Installation: +1 point if the game installs and uninstalls correctly
[/quote]
I wanted to add achivements to my game (like the ones in PS3/XBox games) and I thought about writing a text file and save the progress of the player and the amount of trophies earned in that file. But the thing is that that file would be created in the same directory as the .jar (or any other folder) and if you wish to delete the .jar then the savegame file will remain there (duh) unless you delete it as well. Then I thought that I could simply create a file or modify an existing one within the .jar, and update it while in game. A quick google search on how to do that made me realize that it was too difficult/impossible to do.

I could also use an installer and then the uninstall would take care of the problem of deleting the .jar and the savegame files for the user, but the question remains the same:

Is it possible to create a savegame feature in a game deployed trough an executable .jar while not creating extra files for that purpose?
Thanks!

You could write in the Users’s home directory. System.getProperty(“user.home”) returns the path to the home folder, where you can create a folder for the game and write stuff there.

But, wouldn’t you have to manually delete the game folder after deleting the .jar? ???

Perhaps I didn’t explain very well what my question was (not being a native English speaker doesn’t help either). Basically what I’m asking is if there’s a way to save the state of a game packaged in an executable .jar without creating any other aditional files. And of course, without uploading the data to a server or something like that.

In Android you can save on Bundle. Saving state on desktop absolutely need another file with simple access.

Nope, there is no way you can save into the JAR file, because the file will be in use so you can’t modify it while the JVM is using it.

If you look at how a lot of apps work, they use something in the workspace like: /.minecraft

If you save it to there, you can easily set up a savegame feature and still have updates.

On the desktop there isn’t a way to delete saved files without having an uninstaller. I use this to figure out where to save to:


public static String getDataFolder()
{
   String folder = System.getProperty("user.home");
   String os = System.getProperty("os.name").toLowerCase();
   
   if(os.contains("win"))
   {
      if(os.contains("xp"))
         folder += "\\Application Data\\";
      else if(os.contains("7") || os.contains("vista"))
         folder += "\\AppData\\Roaming\\";
   }
   else if(os.contains("mac"))
      folder += "/Library/Application Support/";
   
   return folder;
}

Don’t forget to put a ‘.’ before the folder name so it would be invisible on Linux so if your game was to be named MyAwesomeGame, the folder would be ‘.myawesomegame’

The ‘.’ works on UNIX-based systems, so it will work on Mac OS X as well. So I’d just do getDataFolder() + “.gamename”. Also, you can hide the folder on windows using this (this method will also work for UNIX based systems):


public static void hideFile(File src)
{
   if(System.getProperty("os.name").toLowerCase().contains("win"))
   {
      Process p;
      try
      {
         p = Runtime.getRuntime().exec("attrib +h " + src.getPath());
         p.waitFor();
      }catch(IOException exc1)
      {
         exc1.printStackTrace();
      }
      catch(InterruptedException exc2)
      {
         exc2.printStackTrace();
      }
   }
   else if(!src.getName().startsWith(".")) // If the file is already hidden to UNIX systems don't add another dot
      src.renameTo(new File(src.getParentFile().getPath() + "/." + src.getName()));
}

Hi

Using only one JAR file as an “executable” file is a bad idea, I already explained it several times on this forum. Unfortunately, on lots of machines, the OS will try to open your JAR with an archiver, it won’t run your game when double-clicking on the JAR. For example, Winzip and WinRAR will do that by default under Windows, Ark will do the same under some GNU Linux distros. I advise you to use another solution like Java Web Start (very easy, allows you to use your JAR but a bit buggy), IzPack (very complete, very professional) or GetDown.

On Windows, just always use the value of the %APPDATA% environment variable, rather than hard-coding what the path will be depending what Windows variant you’re on.


if (os.contains("win")) {
   return System.getenv("APPDATA");
}
else ...

[quote=“gouessej,post:10,topic:38644”]
I was thinking about using Launch4j and create an .exe file from the .jar (that way I can also create an icon for the app :stuck_out_tongue: )

Ok, so there’s no way to implement a savegame feature without creating extra files. Thanks everyone for the code snippets and advices ;D

Or you can create a bat file in same folder as your jar


@echo off
java -jar syalala.jar

However I found some cases where Java not configured on win’s Path. Duh!