Self destruct?

How can I make my game self destruct so I can for example make an autopatcher that deletes the old jar and downloads a new one? I’ve got this so far:


	public static final void destroy() {
		try {
			String command = "PING -n 2 LOCALHOST > NUL&&DEL /Q " + System.getProperty("user.dir") + File.separator + "Renoria.jar";
			Runtime.getRuntime().exec(command);
                        executePatch();                         
		} catch (IOException ex) {
			showError("Patch error", "Failed to delete the old executable");
		}
		System.exit(0);
	}


	public void executePatch() {
        String home = System.getProperty("user.home");
        String dir = home + "/renoriaData";
        File dataFile = new File(dir + "/AutoPatch.jar");
		try {
			Runtime.getRuntime().exec("java -jar \"" + dataFile.getAbsolutePath() + "\"");
		} catch (IOException ex) {
			Logger.getLogger(Console.class.getName()).log(Level.SEVERE, null, ex);
			showError("Patch failed", "Failed to execute the AutoPatcher. Please redownload the client.");
		}
		Runtime.getRuntime().halt(0);
	}

but it doesn’t work :frowning:

Help is appreciated ;D

One more problem:

When I start a new process:


	public static void main(String[] a) {
		Runtime r = Runtime.getRuntime();
		Process p = null;
		try {
			ProcessBuilder proc = new ProcessBuilder("notepad.exe");
			p = proc.start();
			System.exit(0);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

The existing java program does not terminate until notepad is closed and I even called System.exit…

jsut make a webstart

it does that for you :).

you will have to run game with jnlp file though.

If I am correct, it will work offline as well, cause jar file is stored in cache.

haha waste of Bandwidth much? the game is 20mb. Also, its an EXE file so yeah… don’t think that would work .

[quote]old jar and downloads a new one
[/quote]
you said jar. if it is a 20MB download isnt it that way anyways?

mm… I use a java wrapper to wrap it into EXE form then I use a packer to pack the client

I’d make an updater app. You obviously can’t make an app delete itself. So just make a small app that uses java.io.File to remove your old game and then replace it. Easy.

I’d recommend being smart about how you’re packaging these resources, however. It would be best for the user if your EXE had no resources in it and was merely all the class files, instead your auto-updater would download the images/sounds/etc into a local resources folder and then the EXE would reference them. This way updates don’t require the user to download another 20mb, instead you can pick and choose which pieces to replace, and they’ll need to download another like 500kb EXE.

Make a Java application that does nothing but launching another Java application. Keep the Process instance around. Now start polling on a File, to see whether it is updated. If so, call process.destroy() and relaunch. Note that process.destroy() will not run the shutdown hooks.

One note: NEVER EVER replace jars that are used by the JVM. It WILL cause nasty crashes!

  1. put all new and updated files in directory: /pending/
  2. then call process.destroy()
  3. delete the jars and resources in /current/
  4. copy everything from /pending/ into /current/
  5. relaunch the child process with /current/ as working dir

I do keep the resources in another folder. Only some resources are in the JAR, such as the cursor, the Icon, and the splashscreen/loadscreen images it must load before it can unpack the AES archives. I have a small auto updater application(17KB) and I try to make Console.destroy terminate the client, delete the client then launch the Patcher.

Also, I don’t try to make the App delete itself, but in netbeans when I try to terminate the current process and start the Patcher, the current process won’t terminate… I use System.exit(0), by the way.

so

  1. Make it launch another application, so I’m guessing (Start the patch)

Process proc = Runtime.getRuntime().exec("java -jar " + System.getProperty("user.dir") + File.separator + "patch.jar");

  1. Make it poll on the file

new Thread(new Runnable() {
 @Override public void run() {
  File tmp;
  while (true) {
   //Check the file
   if ((tmp = new File(System.getProperty("user.dir") + File.separator + "patch.tmp")).exists()) { //Check patch file
    if (tmp.delete()) {
     break;
    }
   }
   try {Thread.sleep(100) } catch (Exception ex) {}
  }
  proc.destroy();
  Process client = Runtime.getRuntime.exec("start " + System.getProperty("user.dir") + File.separator + "Client.exe");
  System.exit(0);
 }
}.start();
}

Make the patcher poll on the file to see if the client has deleted it


new Thread(new Runnable() {
 @Override public void run() {
  File tmp;
  while (true) {
    if (!(tmp = new File(System.getProperty("user.dir") + File.separator + "patch.tmp")).exists()) {
     break;
    }
    try {Thread.sleep(100) catch (Exception ex) {}
  }
  System.exit(0);
 }
}.start();

Is that what you meant?

process.destroy() != System.exit(0)

Is this what classloaders are for? :slight_smile:

well, is one of the few things that would make us write a custom one…