0

I want to create an installer in java, that copy files from the source (like a packpage to put the files) to the Appdata folder, Is this possible? How can I make this?

TxCraft
  • 35
  • 10

1 Answers1

1
String homeDir = System.getProperty("user.home");
String myAppFolderName = ".MyApp";
Path installDir = Paths.get(homeDir, "AppData");
if (!Files.isDirectory(installDir) { // Maybe not Windows
    installDir = Paths.get(homeDir);
}
Path myAppFolder = Paths.get(installDir.toString(), myAppFolderName);
Files.createDirectory(myAppFolder);

Path sources = Paths.get(new URI("jar:file://... .jar!/install_image"));
Files.copy(sources, myAppFolder);

For a jar's File, URI:

MyAppClass.class.getProtectionDomain()
    .getCodeSource().getLocation().toURI().getPath()

This uses

  • A fall back whenever there is no AppData directory (as on Linux or Mac)
  • Some subdirectory .MyApp to put everything in
  • A zip file system ("jar:file:/...") for the unpacking
  • A way to get the URI of a jar

You'll probably want to capture the case of running without jar too - for development.

Tom
  • 16,842
  • 17
  • 45
  • 54
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138