https://kotlinlang.org logo
#compose-desktop
Title
# compose-desktop
u

姜尚

02/21/2023, 2:29 AM
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

Kirill Grouchnikov

02/21/2023, 4:18 AM
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

姜尚

02/21/2023, 4:48 AM
@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

Kirill Grouchnikov

02/21/2023, 5:29 AM
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

Desmond van der Meer

02/21/2023, 6:47 AM
We use this library to get directories for writing application data (SQLDelight database): https://github.com/harawata/appdirs
a

Arjan van Wieringen

02/21/2023, 7:36 AM
AppDirs as indicated above is a solution that works great
u

姜尚

02/22/2023, 2:10 AM
@Kirill Grouchnikov @Desmond van der Meer @Arjan van Wieringen Thank everyone, It helps me a lot
25 Views