java程序在Eclipse中运行没有问题,打包成.jar文件之后运行后路径出现异常不知道怎么回事?请各位大侠指教

以下是主要代码如下
private void setMapButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int showConfirmDialog = JOptionPane.showConfirmDialog(MapPanel.this, "确定要更换地图?", "友情提示", JOptionPane.YES_NO_OPTION);
if (showConfirmDialog == 0) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {

@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
String fileName = file.getName().toUpperCase();
if (fileName.endsWith(".JPG") || fileName.endsWith(".JPEG") || fileName.endsWith(".GIF")) {
return true;
} else {
return false;
}
}
}

@Override
public String getDescription() {
return "地图格式(.JPG;.JPEG;.GIF)";
}
});
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
//String mapPath = this.getClass().getResource("img/map").getPath();
String mapPath = MapProcessor.class.getResource("/img/map").getPath();
new File(mapPath + "/" + dao.getMapName()).delete();
File selectedMapFile = fileChooser.getSelectedFile();
File upLoadFile = new File(mapPath + "/map" + selectedMapFile.getName().substring(selectedMapFile.getName().lastIndexOf(".")));
dao.setMapName(upLoadFile.getName());
try {
InputStream inStream = new FileInputStream(selectedMapFile);
OutputStream outStream = new FileOutputStream(upLoadFile);

int readBytes = 0; // 读取字节数

byte[] buffer = new byte[1024]; // 定义缓存数组

while ((readBytes = inStream.read(buffer, 0, 1024)) != -1) {// 从输入流读取数据到缓存数组中

outStream.write(buffer, 0, readBytes); // 将缓存数组中的数据输出到输出流

}

outStream.close();// 关闭输出流对象

inStream.close();// 关闭输入流对象

} catch (Exception ex) {
Logger.getLogger(MapPanel.class.getName()).log(Level.SEVERE, null, ex);
}
MapPanel.this.mapProcessor.replaceMap("/img/map/" + upLoadFile.getName());
slider.setValue(slider.getValue() + 1);
smallMapLabel.refreshScale();
smallMapLabel.setIcon(new ImageIcon(mapProcessor.zoom(200, 200)));
}
}
}

以下是在控制台出现的异常
D:\>java -jar MapModule.jar
五月 02, 2012 5:07:50 下午 com.mwq.map.MapPanel setMapButtonActionPerformed
严重: null
java.io.FileNotFoundException: file:\D:\MapModule.jar!\img\map\map.jpg (文件名、
目录名或卷标语法不正确。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.mwq.map.MapPanel.setMapButtonActionPerformed(MapPanel.java:387)
at com.mwq.map.MapPanel.access$20(MapPanel.java:352)
at com.mwq.map.MapPanel$21.actionPerformed(MapPanel.java:322)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

如果只是读取的话解决很简单,读取jar文件中的内容是有特定语法的。
做个比较
1.普通windows文件系统:
file:\D:\MapModule\img\map\map.jpg //你一定要在这个路径上有文件才能读写
2.jar文件中的文件:
jar:file:\D:\MapModule.jar!\img\map\map.jpg
区别一眼就看出来了(2中有jar前缀,并且有'!')。
给你一个获取输入流的方法:
URL url=new URL("jar:file:\D:\MapModule.jar!\img\map\map.jpg");
InputStream is=url.openStream();

但我看你的意思是要往jar里面写入数据。
我不建议这么做,你最好把上传的文件放到另一个文件或文件夹中,就像没有哪个exe文件把上传的文件放到自己的exe中。我没这么做过,如果你硬要往里写肯定会遇到“文件已在另一个系统中打开”之类的错误。
如果你喜欢写到jar这种压缩文件中其实不难,jar就是用的zip压缩,具体内容一查就了解了。
我还是建议你构造一个合理的目录结构,比如一般的游戏:
./map/*
./data/*
./save/*
./start.jar
这样不是挺好的吗追问

谢谢啊,分析的恨透侧。我确实是要往jar包里面写数据,那就我当前的目录结构能写进去吗?那要是能写的话,如何获取他的路径呢,或者说怎样构造目录才能写进去呢,在eclipse中调试的获取的路径,打包之后都会出异常。如果像你
“URL url=new URL("jar:file:\D:\MapModule.jar!\img\map\map.jpg");
InputStream is=url.openStream() “
构造的话,那要是目录改了的话也重新要改代码,那在eclipse中肯定就没法运行了

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-05-02
错误信息上说,你的MapPanel类里面的387行的
setMapButtonActionPerformed()方法找不到\D:\MapModule.jar!\img\map\map.jpg这个文件。
你看看是不是你的路径有问题。
你把盘符D前面的 “ \ ” 去掉试一试。或者是map.jpg这个文件损坏。追问

首先谢谢啊,嗯是的,387行就是这段代码里面的,应该是这个mapPath路径的问题,我是通过String mapPath = MapProcessor.class.getResource("/img/map").getPath();获得的路径,在eclipse中获得的路径是/D:/Administrator/workspace/MapModule/bin/img/map
不过打包成.jar文件之后就变成了\D:\MapModule.jar!\img\map\,我看到网上有人说是打包成jar包之后,只能读不能写,也不知道怎么回事

追答

是不是因为你使用的是绝对路径所以找不到,
你用相对路径试试。然后,
jar包只是一种压缩格式,应该不会吧。
ps:我也是菜鸟,看来我帮不了你了。
你在自己折腾吧,祝你好运了。

第2个回答  2012-05-02
我遇到过这个问题,private ImageIcon imageIconP1=new ImageIcon(this.getClass().getResource("bj2.jpg"));//这样的话打成jar包能显示图片在eclipse里不能运行,private ImageIcon imageIconP1=new ImageIcon("bj2.jpg");//这样的话在eclipse里能运行。(我只是说图片路径要这么写,不一定要用ImageIcon)。肯定没问题……追问

不太明白,那这个到地该如何获取路径呢,找了各种方法获取路径打成jar包之后总会有异常,总是那几句话有问题

第3个回答  2012-05-03
注意一下路径大小写问题。
eclipse调试环境会忽略这个问题。~
相似回答