【问题标题】:Program crashing when reading an image within a .jar file读取 .jar 文件中的图像时程序崩溃
【发布时间】:2014-07-01 15:59:21
【问题描述】:

好的,所以我有一个程序,您可以在其中上传出租房屋的图像,然后将图像复制到所有图像所在的目录。然后在另一个窗口中,它将所有图像加载到我可以单击的演示文稿中。在通过 Sublime Text 编译和运行时,它工作得非常好。但是在创建一个 .jar 文件并运行它之后,出现了一些问题。我可以上传图像并将其复制到图像目录中,但是当我转到应该显示所有图像的另一个 JFrame 窗口时,它会锁定程序并且必须通过任务管理器关闭。有什么建议?

这是启动程序时的代码,其中一些房屋的库存图像被加载到所有房屋图像的主图像目录中:

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 );

}

然后我有一个很长的 Image 类,我在 PasteBin 上发布了代码:

http://pastebin.com/yjzZdVTC

【问题讨论】:

  • 更相关的是事件处理代码。如果操作不当,UI 线程可能会被锁定。

标签: java image jar


【解决方案1】:

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。一般来说,你应该处理它。

恕我直言...

【讨论】:

  • 感谢您对例外情况的看法。我对 try-catch 方法仍然很陌生。关于1),这也是我的猜测。当它作为 .jar 运行时,如何告诉它从我的硬盘驱动器上的目录打开?我的 .jar 文件与我在 Sublime 中运行的 java 文件位于同一目录中。
【解决方案2】:

如果您没有抛出任何异常并且您的应用程序涉及带有 Swing 的 GUI,这可能是因为并发性。

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

仔细阅读。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    相关资源
    最近更新 更多