2016-05-23 51 views
1

我在src文件夾(src/images)下有圖像文件夾。當我在eclipse中運行程序時,程序運行良好,但是當我導出可運行jar文件並嘗試運行此操作時,我看到錯誤。我寫在下面:爲什麼在導出的jar文件中找不到src/images路徑

java.io.FileNotFoundException: src\images\test2.bmp (The system cannot find the 
path specified) 

我的程序:

public class FirstSWTExample { 
public static void main(String[] args) { 
    . 
    . 
    saveImage(); 
    Image image=new Image(display, "src/images/test2.bmp"); 
    shell.setImage(image); 
    } 
public static void saveImage() { 
    String s="...."; 
    byte[] dataCustImg = Base64.decode(s.getBytes()); 

    try { 

     OutputStream stream = new FileOutputStream("src/images/test2.bmp"); 

     stream.write(dataCustImg); 
     stream.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 
} 

我覺得沒有找到jar文件images文件夾。當我解壓縮jar文件時,找不到圖像文件夾。我如何解決我的問題?如果我的問題很簡單,我是java的初學者。

+0

你試試這個?'ImageIO.read(的getClass()。的getResource( 「/圖片/ test2.bmp」)) ;' –

+0

Thnaks。但如何將它設置爲我的shell?我是java中的新手 – Fahim

+0

當你在eclipse中運行你的程序時,有一個相對目錄'src',你的程序使用它。如果你想使用jar資源,你必須加載爲'getClass()。getResource(PATH)',但你可以改變它,看看這個:http://stackoverflow.com/questions/2797367/write-to-a -file-stream-returned-from-getresourceasstream –

回答

0

問題可能出在文件路徑上。

這可能不是最好的解決辦法,但你可以嘗試使用絕對路徑

"C:\\projectName\\src\\images\\test2.bmp" 
+1

謝謝,但我想在任何計算機上運行此程序,也許有些計算機沒有C驅動器 – Fahim

+0

然後,你必須提供你的圖像文件夾與你的罐子 – Supahupe

相關問題