簡體   English   中英

Java項目在Eclipse中運行,但在jar文件中不正確

[英]java project runs in eclipse but not correct in jar file

我面臨着奇怪的問題。 當我在Eclipse中運行我的項目時,一切正常。 當我將項目導出到可執行的jar文件中並運行它時,它只會加載純JFrame

我應該如何發現問題? 我不知道有什么問題。 我認為問題可能在這里,但不知道:

try {
    URL url = getClass().getClassLoader().getResource("maps/"+ level +".txt");
    File file = new File(url.getFile());
    fr = new FileReader(file);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
    in = new BufferedReader(fr);

您閱讀資源的方式不正確。 嘗試使用類似:

    BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/maps/1.txt")));
    br.readLine();

嘗試使用以下解決方案,System.getProperty()將為您提供當前目錄路徑:

-代碼段:

InputStream input = null;
try{
    String dirPath = System.getProperty("user.dir");
    String completePath= dirPath+fileName;
    input = this.getClass().getClassLoader().getResourceAsStream(completePath);
}catch(IOException ioex){
e.printStackTrace();
}

暫無
暫無

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

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