Incorrect file name, directory name, or volume label syntax - Kotlin - Maven

I get this error when I start my packaged jar. When I run it from my IDE, it does not give an error and does not start.

java.io.FileNotFoundException: file:\C:\Development\Kotlin\AccountTool\target\AccountTool-1.0-SNAPSHOT-jar-with-dependencies.jar!\accounts.json (The filename, directory name or volume label syntax is incorrect)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileReader.<init>(Unknown Source)
    at com.martacus.accounttool.ToolView$handler.readData(Tool.kt:41)
    at com.martacus.accounttool.ToolView.<init>(Tool.kt:56)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at tornadofx.FXKt.find(FX.kt:238)
    at tornadofx.App.start(App.kt:27)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$52/31866147.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/2900468.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/4210449.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/24077489.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/1828305.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

The hierarchy of code and files in the idea: enter image description here

fun readData(){
    accounts.clear()
    var readFile = ToolView::class.java.classLoader.getResource("accounts.json").file
    println(readFile)
    FileReader(readFile).use{
        var account = gson.fromJson(it, Array<Account>::class.java) ?: return
        for(i in account){
            accounts.add(i)
        }
    }

}

.jar root:

enter image description here

I have no idea what is going wrong and why this gives me an error. A search in stackoverflow and google did not give me the correct answer. I hope someone here can help me, if you need more information, tell me about it.

Thank!

+4
source share
1 answer

, . API File() FileReader() .

Class.getResourceAsStream ClassLoader.getResourceAsStream :

val stream = ToolView::class.java.classLoader.getResourceAsStream("accounts.json")

stream.reader().use {
    println(it.readText())
}
+5

Source: https://habr.com/ru/post/1650561/


All Articles