App con't create file in it workdir when I install...
# compose-desktop
u
App con't create file in it workdir when I installed app on Windows Program Files Dir How do I create data files for my application when installing to windows🐒
k
You need to provide more information. What is a workdir? How are you creating a file? What is the error message that you see, or an exception? What version of Windows is it? Are you running in a virtual environment, or with full admin privileges? These one-liner questions rarely go answered, since critical information is lacking.
u
@Kirill Grouchnikov
Copy code
// Here is my code
fun loadingConfig() {
    val file = File("config.json")
    if (!file.exists()) {
        try {
            val content = "{}"
            file.createNewFile()
            file.writeText(content)
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
    val content = readFile("config.json")
}
fun main() {
    loadingConfig()
}

// I packaged and installed my app
// I started him using the command line
CMD
➜  pwd

Path
----
C:\Program Files\Helper


➜  .\Helper.exe
java.io.IOException: 拒绝访问。 // It means to deny access
        at java.base/java.io.WinNTFileSystem.createFileExclusively(Native Method)
        at java.base/java.io.File.createNewFile(Unknown Source)
        at com.sun.helper.MainKt.loadingConfig(Main.kt:93)
        at com.sun.helper.MainKt.main(Main.kt:217)
        at com.sun.helper.MainKt.main(Main.kt)
Exception in thread "main" java.io.FileNotFoundException: config.json (系统找不到指定的文件。)
        at java.base/java.io.FileInputStream.open0(Native Method)
        at java.base/java.io.FileInputStream.open(Unknown Source)
        at java.base/java.io.FileInputStream.<init>(Unknown Source)
        at com.sun.helper.common.FileKt.readFile(file.kt:12)
        at com.sun.helper.MainKt.loadingConfig(Main.kt:99)
        at com.sun.helper.MainKt.main(Main.kt:217)
        at com.sun.helper.MainKt.main(Main.kt)
Failed to launch JVM
Child process exited with code 1
k
I sure hope the JVM doesn't get permission to write random files under C:\Program Files
You can try getting
System.getProperty("user.home")
for a folder that works across different OSes.
And then from there decide on your data folder structure
d
We use this library to get directories for writing application data (SQLDelight database): https://github.com/harawata/appdirs
a
AppDirs as indicated above is a solution that works great
u
@Kirill Grouchnikov @Desmond van der Meer @Arjan van Wieringen Thank everyone, It helps me a lot