文件名、目录名或卷标语法不正确 - Kotlin - Maven

The filename, directory name or volume label syntax is incorrect - Kotlin - Maven

我在启动打包的 jar 时遇到此错误。当我从我的 IDE 运行 它没有给出错误和 运行.

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$launchApplication19(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda/31866147.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda/2900468.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null0(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda/4210449.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater1(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda/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$null5(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda/1828305.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

ide 中的代码和文件层次结构:

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 根目录:

我不知道 ide 出了什么问题,为什么会给我这个错误。在 Whosebug 和 google 上搜索并没有给我 ide 正确的答案。我希望这里有人可以帮助我,如果您需要更多信息,请告诉我。

谢谢!

当资源被打包到 jar 中时,它不再作为文件系统中的文件可用。因此,您不能使用标准 API 来使用文件系统作为 File()FileReader() 来访问资源的内容。

相反,您应该使用 Class.getResourceAsStream or ClassLoader.getResourceAsStream 方法打开资源流并从该流中读取内容:

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

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