2014-05-13 55 views
2

好的,所以我有一個程序可以上傳房屋的圖像以供租借,然後將圖像複製到所有圖像所在的目錄中。然後在另一個窗口中加載所有可以點擊的圖像。這在編譯和通過Sublime Text運行時工作得很好。但是在創建一個.jar文件並運行後,會出現一些問題。我可以上傳一個圖像並將其複製到圖像目錄中,但是當我到另一個應該呈現所有圖像的JFrame窗口時,它會鎖定程序並且必須通過任務管理器關閉。有什麼建議麼?在.jar文件中讀取圖像時程序崩潰

public void copyStockImages(String filename) 
{ 

    String filepath = "Images/Testimages/" + filename; 
    BufferedImage image = null; 

    try { 
     image = ImageIO.read(getClass().getResource(filepath)); 
    } 
    catch(FileNotFoundException fnfe){ 
     System.out.println("File not found: " + filepath); 
    } 
    catch(IOException ioe){ 
     System.out.println("Error reading image"); 
    } 

    String newFilePath = "Images/houseImages/" + filename; 
    File fileOut = new File(newFilePath); 
    if(fileOut.exists()){ 
     return; 
    } 
    try{ 
     ImageIO.write(image, "jpg", fileOut); 
    } 
    catch(IOException ioe){ 
     System.out.println("Error reading image"); 
    } 
} 

上傳圖片:

private void uploadImages(HouseForRent house) { 

    if(filepaths.isEmpty()) { 
     return; 
    } 

    Iterator<String> iter = filepaths.iterator(); 

    while(iter.hasNext()) { 

     String filepath = i.next(); 
     String filetype = findFiletype(filepath); 

     BufferedImage image = null; 

     try { 
      image = ImageIO.read(new File(filepath)); 
     } 

     catch(FileNotFoundException fnfe) { 
      dialog("Cannon find file: " + filepath); 
     } 

     catch(IOException ioe) { 
      dialog("Error reading file"); 
     } 

     catch(NullPointerException npe) { 
     } 

     try { 
      saveImage(image, filetype, house); 
     } 

     catch(IOException ioe) { 
      dialog("Error reading from file: " + ioe.toString()); 
     } 

     catch(NullPointerException npe) { 
      dialog("Error: " + npe.toString()); 
     } 
    } 
} 

複製

啓動程序,其中庫存圖片一些房屋的被裝載到主圖像目錄房屋的所有圖像時,下面的代碼圖像轉到目錄:

private void saveImage(BufferedImage image, String filetype, HouseForRent house) throws IOException{ 

    String newFilePath = "Images/houseImages/"; 


    String filelink = newFilepath + filetype; 

    File fileOut = new File(filelink); 

    if(fileOut.exists()) { 

     house.addImage(filelink); 
     return; 
    } 

    try { 
     ImageIO.write(image, "jpg", fileOut); 

    } 
    catch(Exception e) { 

     dialog("Error reading image"); 
    } 

    house.addImage(filelink); 

    imageLinks.add(filelink); 

} 

然後我有這個長的圖像類我發佈的代碼在引擎收錄:

http://pastebin.com/yjzZdVTC

+0

更相關的將是事件處理代碼。如果你做得不當,UI線程可能被鎖定。 –

回答

2

1)我懷疑這是你的問題的來源:getClass().getResource(filepath)

String filepath = "Images/Testimages/" + filename;將從IDE(如Eclipse)執行時成爲硬盤驅動器上的路徑。但是,當您從.jar執行時,它將嘗試在您的.jar文件中打開文件

2)catch(IOException ioe) { dialog("Error reading file");是「次優」。保留至少錯誤消息(ioe.getMessage())總是一個好主意。如果不在你的用戶對話框中,那麼可能在某個地方的日誌中。

3)catch(NullPointerException npe) { }是一個真的不好主意。你真的不應該像這樣壓制一個NullPointerException。一般來說,你應該處理它。

恕我直言......

+0

非常感謝您的意見。我仍然嘗試着使用try-catch方法。關於1),這也是我的猜測。如何將它作爲.jar運行時告訴它從硬盤上的目錄中打開?我在Sublime中運行的java文件所在的目錄中有.jar文件。 – user3633727

+0

問題解決了,你的第一點是這個問題。謝謝! – user3633727

相關問題