Simple .JAR Updater

Hello JGO, I got rather bored last night so I decided to program a “Game Updater”.

Earlier today I implemented alot more, so I figured I’d release it with the new updates:

  • Calculating the current download percentage.
  • Calculating the current KBS. (Kilobytes downloaded per second)
  • Calculating the current ETA. (Estimated time of arrival in seconds)
  • Constructor parameters for easier implementation.

Notes:
You must have a server to upload the files to E.G: http://www.yourwebsite.co.nf/upload_directory/.
In the upload directory should be your most recent .JAR of your game along with a .txt file in the same directory that should only contain your games current version E.G: “0.0.0.1” saved in “version.txt” file.
The .JARs name should be formatted like so: Game_Name_Here_0.0.0.1 (0.0.0.1 being the most recent version)
So everytime you update your game and pack it into a .JAR you need to upload the .JAR to the directory on your server and update the version number on the .JAR and inside the .TXT file :slight_smile:

Usage:


	/**
	 * Main entry point into the application
	 * @param args The arguments passed via the user (NONE)
	 * @throws IOException There was a error during reading / writing.
	 * @throws MalformedURLException URL is missing headers?
	 */
	public static void main(String[] args) throws MalformedURLException, IOException {
		/* Parameters for the GameUpdater constructor */
		String currentClientVersion = "0.0.0.1"; // the current version programmed into the client.
		String versionDirectoryURL = "http://www.javatools.co.nf/releases/"; // the directory of your recent .JAR/.TXT.
		String versionFileNameAndExt = "version.txt"; // the name of the version file / extension.
		String gameName = "Block_Tamer_Client_"; // the name of your game ^_^
		GameUpdater updater = new GameUpdater(currentClientVersion, versionDirectoryURL, versionFileNameAndExt, gameName);
		if (updater.needsUpdating()) {
			updater.downloadUpdatedJar(true); // true to print percentage while downloading
		}
	}

There are a few methods (Getters) implemented to make this game more easily implementable in your game.
(While updating you can show the ETA/KBS/percentage etc)


public int getDownloadPercentage() {
	return downloadPercentage;
}

public double getETA() {
	return ETA;
}

public int getKbs() {
	return kbs;
}

Anyhow thanks for reading guys, here is the source code:
http://www.java-gaming.org/?action=pastebin&id=602

I think you should add support for updating via the update date, so that people don’t have to increase the version for an update if they don’t want to (it seems illogical, but if you are letting people play when you are not even ready to put a version number on the game it makes sense).

Also, it seems counter-intuitive to code the version of the client into the client. I guess this doesn’t require a separate launcher program, but if it were a launcher it would be more convenient to hold the version number in a file and update and check the file as necessary. Oh wait, it sounds like you put it in the client and the text file, then it’s okay.

It sounds better than my similar library, so nice job!

That would be the way to go yes, or I could’ve compared the current .JAR size with the most recent .JAR size or went by dates like you suggested :slight_smile:

This is just a little class i threw together in under a hour maybe so it’s not super fancy :stuck_out_tongue:
Barely even documented D:

Thanks for the feedback mate, I hope some people make use of this :slight_smile:

If it doesn’t get used at all, don’t worry about it. My library didn’t get used at all except by me and it worked extremely well for me. Even if other people don’t use it, if you can make this work extremely well for you then it is completely worth it :smiley:

Well, to be frank I have no use for this myself at the moment lol.

I programmed this version http://pastebin.com/6gu9ad63# for a member of JGO last night.
Earlier this morning I than implemented the KBS/ETA/Percentage etc and figured I’d release it before deleting it in hopes of other JGO members / web searchers finding a use for it :slight_smile:

Well, if it helps, I will definitely use its code as a reference to add a KB/s and ETD (estimated time done) to my library. It’s very well documented compared to mine, which will be very helpful. Thanks!

The thing is, compared to writing a game, an auto-updater/launcher is extremely easy.
Which explains why no-one else uses it.

As in, a few files:

Main class - Manages the whole patching/launching process.
GUI handler.
Downloader.
Extractor (because I package in a .zip).
A class for loading and displaying an image in the GUI.
A separate class that writes the version number into a binary file for when I increment the version.

The only things missing is calculating the download size, and I personally don’t need that. (I might add it anyway though)

EDIT: I’ll give you appreciation for working out the download size. That was useful.

Rawr, It’s implemented already BTW :slight_smile:


	/* Obtaining the files size in kilobytes */
	private Double getURLSizeInKB(String urlStr) {
		double contentLength = 0;
		try {
			URL url = new URL(urlStr);
			HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
			contentLength = httpConn.getContentLength();
			httpConn.disconnect();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return contentLength;
	}

Thanks for the feedback mate :slight_smile:

I meant the only thing missing in my patcher. ;D

Oh great lol. 8)
If people find this post and manage to find something to rip: yay for them :>
Mission complete.

Most of this code (ETA/KBS/Percentage) came out of a FileDownloader I made a while ago :stuck_out_tongue:
Source code for that is here: http://www.java-gaming.org/?action=pastebin&id=603

I have a list of errors that need to be handled while downloading:

  • ConnectException - Can’t connect to the server. Probably because the server is down?
  • UnknownHostException - Can’t find the file on the server. Possibly because of no internet connection. Possibly incorrect URL.
  • FileNotFoundException - No file associated with the URL. Maybe the file was removed or maybe the URL is wrong.
  • MalformedURLException - Incorrect URL. (syntax, not missing link)

Found by turning my internet connection on/off while downloading. :slight_smile:

Thanks mate, the only exception I managed to ‘force’ while fiddling with my internet was a:


java.net.UnknownHostException: www.javatools.co.nf

And that only occurred while launching the updater with no internet lol.

I also launched the updater and killed the internet half way through the download and no exceptions occured :confused:
It just paused the download lol 0_0!

Thanks for the feedback :slight_smile:

Breaking the download mid-way through is the most evil thing ever. It most of the time seems to stop the download rather than just pause it.

@HeroesGraveDev talking about the necessary files: I don’t think that having a GUI handler, extractor, or image display classes are necessary. If you are just creating the framework to easily update files, then the person using it may want to make a custom GUI for it, or may not want one at all.

I wasn’t referring to the framework.
I was referring to a Launcher/Patcher as a whole.