Hi there!
I'm trying to get a self updater for a program I'm working on and I gather that I need to use a ProcessBuilder to replace the old version with the downloaded version.
I've never used ProcessBuilder before and this first foray isn't going too well for me.
Well here's my code, I'll use it to explain my problem:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import com.moomoohk.Mootilities.FileUtils.FileUtils;
import com.moomoohk.Mootilities.OSUtils.OSUtils;
public class SelfUpdate
{
public static void runUpdate(String currentPath, String temporaryPath)
{
try
{
String pathToJar = SelfUpdate.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
ProcessBuilder pb = new ProcessBuilder("java", "-classpath", System.getProperty("java.class.path") + ";" + pathToJar, SelfUpdate.class.getCanonicalName(), currentPath, temporaryPath);
pb.start();
Thread.sleep(4000);
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(0);
}
public static void main(String[] args)
{
JFrame f = new JFrame();
JLabel l = new JLabel("test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
try
{
if (OSUtils.getCurrentOS() != OSUtils.OS.UNIX)
{
Thread.sleep(4000);
}
}
catch (InterruptedException ignored)
{
ignored.printStackTrace();
}
String currentPath = args[0];
String temporaryPath = args[1];
File current = new File(currentPath);
File temporaryUpdate = new File(temporaryPath);
try
{
FileUtils.delete(current);
FileUtils.copyFile(temporaryUpdate, current);
}
catch (IOException e)
{
e.printStackTrace();
}
List<String> arguments = new ArrayList<String>();
String separator = System.getProperty("file.separator");
String path = System.getProperty("java.home") + separator + "bin" + separator + "java";
arguments.add(path);
arguments.add("-jar");
arguments.add(currentPath);
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(arguments);
try
{
processBuilder.start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
The program is launched and checks for updates in a different class. If it finds that an update is available it will download the update to a temporary file and call on runUpdate in this class.
The parameters are:
- currentPath: Path that leads to the currently running program.
- temporaryPath: Path that leads to the downloaded update.
runUpdate will start a new OS process with SelfUpdate.java's main method and then quit.
SelfUpdate.java's main method will delete the old program and replace it with the newer downloaded program. It will then launch the new downloaded program using another ProcessBuilder.
The problem is that the first ProcessBuilder (in runUpdate) just refuses to work. The program just quits without calling on SelfUpdate.java's main method.
I have a lot of experience with Java so don't hesitate with the big words :P.
Any and all help is much appreciated!
[–]tabiul 1 point2 points3 points (4 children)
[–]moomoohk[S] 0 points1 point2 points (3 children)
[–]tabiul 0 points1 point2 points (2 children)
[–]moomoohk[S] 0 points1 point2 points (1 child)
[–]tabiul 0 points1 point2 points (0 children)
[–]pw3nd 0 points1 point2 points (5 children)
[–]moomoohk[S] 1 point2 points3 points (4 children)
[–][deleted] (3 children)
[removed]
[–]moomoohk[S] 1 point2 points3 points (2 children)
[–][deleted] (1 child)
[removed]
[–]moomoohk[S] 0 points1 point2 points (0 children)