2016-08-08 42 views
4

當我啓動打包的jar時出現此錯誤。當我從IDE運行它時,它不會給出錯誤並運行。文件名,目錄名稱或卷標語法不正確 - Kotlin - Maven

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) 

代碼和文件層次的IDE: 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根:

enter image description here

我不知道發生了什麼錯誤,爲什麼它給我錯誤。在stackoverflow上搜索和谷歌沒有提供我正確的答案。我希望這裏有人能幫助我,如果你需要更多的信息,請說出來。

謝謝!

回答

5

當資源打包到jar中時,它不再作爲文件系統中的文件。因此,您不能使用標準API來處理文件系統,如File()或​​來訪問資源的內容。

相反,你應該打開資源流與Class.getResourceAsStreamClassLoader.getResourceAsStream方法和讀取該流的內容:

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

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

它可能還需要與前綴'/''爲/ accounts.json',因爲它是與ToolView不在同一個包中(然後我不知道ToolView在哪個包中)。或者改用系統類加載器。 –

+0

@JaysonMinard我相信在'Class'實例上調用'getResourceAsStream'時,需要解析資源相對於其包的名稱。這裏'getResourceAsStream'在'ClassLoader'上被調用。但無論如何,謝謝澄清。 – Ilya

+0

謝謝!有用!但是這也適用於WriteFile嗎?由於我沒有改變,所以在寫作時我確實得到了錯誤。儘管我沒有看到stream.writer()方法。 – Martacus

相關問題