Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (591)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Self destruct?  (Read 2121 times)
0 Members and 1 Guest are viewing this topic.
Offline Renoria

Junior Devvie




...


« Posted 2009-03-30 10:25:20 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
   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);
   }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   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 Sad

Help is appreciated Grin

One more problem:

When I start a new process:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   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..
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #1 - Posted 2009-03-30 12:44:33 »

jsut make a webstart

it does that for you Smiley.

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.
Offline Renoria

Junior Devvie




...


« Reply #2 - Posted 2009-03-30 13:49:25 »

haha waste of Bandwidth much? the game is 20mb. Also, its an EXE file so yeah.. don't think that would work .
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #3 - Posted 2009-03-30 14:21:57 »

Quote
old jar and downloads a new one

you said jar. if it is a 20MB download isnt it that way anyways?
Offline Renoria

Junior Devvie




...


« Reply #4 - Posted 2009-03-30 15:03:59 »

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
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2009-03-30 17:07:29 »

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.

See my work:
OTC Software
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 836
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2009-03-30 19:33:03 »

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


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Renoria

Junior Devvie




...


« Reply #7 - Posted 2009-03-31 10:41:45 »

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.

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.

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



so

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

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


2) Make it poll on the file

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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();
}


3)

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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?
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 836
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2009-03-31 18:19:11 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Mr_Light

Senior Devvie


Medals: 1


shiny.


« Reply #9 - Posted 2009-03-31 23:01:42 »

Is this what classloaders are for?  Smiley


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

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Gibbo3771 (10 views)
2014-11-24 19:59:16

trollwarrior1 (35 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!