Ahmed Riyadh
06/19/2024, 6:45 PMephemient
06/19/2024, 7:27 PMPHondogo
06/19/2024, 9:27 PMAhmed Riyadh
06/20/2024, 12:33 PMI'm not sure what file locking the JVM does on Windows, but usually you should be able to rename a file to replace it even if it's in useThis file locking is something specific to Windows and not a restriction by the JVM, otherwise renaming the current running JAR file on Linux and macOS works pretty well
Ahmed Riyadh
06/20/2024, 12:45 PMbuildSrc
, another issue that we have autoUpdate
preference in configurations file and data classes that are specific to the script, we already have common module however we prefer to not complicate things more as it require more setup to get it working from the user side. and what if there is a update to the auto updater launcher that fix bugs or add new features, we would prefer to not complicate things too much.
I already tried common solutions like deleteOnExit
or addShutdownHook
Ahmed Riyadh
06/20/2024, 1:17 PMAhmed Riyadh
06/20/2024, 1:44 PMif (!OperatingSystem.current.isWindows()) {
Runtime.getRuntime().addShutdownHook(
Thread {
currentRunningJarFile.delete()
newJarFile.renameTo(currentRunningJarFile)
},
)
} else {
// On Windows, we can't rename, delete or modify the current running JAR file due to file locking
val updateBatScript =
SyncScriptInstanceFiles.SyncScriptData.Temp.file
.resolve("update.bat")
withContext(Dispatchers.IO) {
updateBatScript.parentFile.mkdirs()
updateBatScript.createNewFile()
}
updateBatScript.writeText(
"""
@echo off
timeout /t 2 > nul
del "${currentRunningJarFile.absolutePath}"
move "${newJarFile.absolutePath}" "${currentRunningJarFile.absolutePath}"
exit
""".trimIndent(),
)
ProcessBuilder("cmd", "/c", "start", updateBatScript.absolutePath).start()
}
exitProcess(0)
It cause other issues as I have to close the process with 0, which indicate success, 1 indicate failure, which cause a bug with the launcher that use this JAR, however this issue is not directly related to this Thread.
The code can still be improved, for now a workaround for the issue above is to exit with 1 and ask the user to launch once again.