簡體   English   中英

運行jar文件時出現NoSuchFileException

[英]NoSuchFileException when running jar file

當我運行訪問src目錄中其他文件的jar文件時,出現以下RuntimeException

線程“主”中的異常java.nio.file.NoSuchFileException:src \\ FileToBeAccessed.txt.lck,位於sun.nio.fs.WindowsException.translateToIOException(未知源),位於sun.nio.fs.WindowsException.rethrowAsIOException(未知源) java.nio.channels.FileChannel.open上的sun.nio.fs.WindowsFileSystemProvider.newFileChannel(未知源)處的java.nio.channels.FileChannel處的sun.nio.fs.WindowsException.rethrowAsIOException(未知源)。 java.util.logging.FileHandler中的open(未知源).java.util.logging.FileHandler中的openFiles(未知源).Program.main(Program.java:30)中的(未知源)

當我直接運行而不構建jar文件時,代碼工作正常。

您需要更改將jar中的資源訪問流的方式。

this.getClass().getResourceAsStream("/file.txt");

正確的方法是

this.getClass().getResourceAsStream("file.txt");

真的很奇怪

this.getClass().getResource("file.txt")

可以在IDE中工作,但在jar中運行時卻不能。

this.getClass().getResourceAsStream("file.txt") 

在IDE內部和jar中均可使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM