سؤال

Ok, so I'm having a program where you can upload images of a house for rent, and the image is then copied over to a directory where all images are. Then in another window it loads all the images to presentation that I can click on. This is working perfectly fine when compiling and running through Sublime Text. But after creating a .jar file and running it, there's some problems. I can upload an image and copy it over to the image directory fine, but when I go to the other JFrame-window where all the images are supposed to be presented, it locks the program and have to be closed through task manager. Any suggestions?

Here's the code when starting the program, where stock images of some houses are loaded into the main image directory for all images of houses:

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

Upload images:

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

Copying the image over to a directory:

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

}

Then I have this long Image class which I posted the code at PasteBin:

http://pastebin.com/yjzZdVTC

هل كانت مفيدة؟

المحلول

1) I suspect this is the source of your problem: getClass().getResource(filepath).

String filepath = "Images/Testimages/" + filename; will be a path on your hard drive when you execute from an IDE (like Eclipse). However, it will try to open a file in your .jar file when you execute from a .jar.

2) catch( IOException ioe ) { dialog( "Error reading file" ); is "sub-optimal". It's always a good idea to preserve at least the error message (ioe.getMessage()). If not in your user dialog, then perhaps in a log somewhere.

3) catch(NullPointerException npe) { } is a really bad idea. You really shouldn't squelch a NullPointerException like that. In general, you should deal with it.

IMHO...

نصائح أخرى

If you are not having any Exceptions being thrown and your application involves a GUI with Swing, it is probably because of concurrency.

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

Read up on this.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top