2016-05-23 50 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

相关问题