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?
Asked
Active
Viewed 70 times
0
-
[http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html). – azurefrog Oct 06 '15 at 14:42
-
http://stackoverflow.com/questions/3509562/how-to-make-installer-pack-of-java-swing-application-project – Pat B Oct 06 '15 at 14:43
-
I will try this, tank you – TxCraft Oct 06 '15 at 14:57
1 Answers
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