java程序在读取内存中的图像时崩溃。jar文件
好的,我有一个程序,你可以上传一栋房子的图片供出租,然后把图片复制到一个目录下,所有图片都在那里。然后在另一个窗口中,它会将所有图像加载到演示文稿中,我可以单击它。在编译和运行Sublime文本时,这是非常好的。但是在创建了一个。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 );
}
然后我有一个长图像类,我在PasteBin上发布了代码:
# 1 楼答案
1)我怀疑这就是你问题的根源:
getClass().getResource(filepath)
String filepath = "Images/Testimages/" + filename;
将是从IDE(如Eclipse)执行时硬盘上的路径。但是,它会尝试在您的应用程序中打开一个文件。jar文件当您从。罐子2)
catch( IOException ioe ) { dialog( "Error reading file" );
是“次优的”。最好至少保留错误消息(ioe.getMessage())。如果不是在用户对话框中,那么可能是在某个地方的日志中3)
catch(NullPointerException npe) { }
真是个坏主意。你真的不应该压制这样的NullPointerException。一般来说,你应该处理它嗯
# 2 楼答案
如果您没有抛出任何异常,并且您的应用程序涉及带有Swing的GUI,那么这可能是因为并发性
http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html
请仔细阅读