java程序在Eclipse中运行没有问题,打包成.jar文件之后运行后路径出现异常不知道怎么回事?请各位大侠指教
本帖最后由 wudangsanxia 于 2012-05-02 17:28:20 编辑 在eclipse中运行时mapPath的值为/D:/Administrator/workspace/MapModule/bin/img/map,大体意思是将mapPath路径下的map.jpg的删除,然后重新读入选择的图像文件map.jpg或者map.gif或者map.jpeg,存入到mapPath路径下,eclipse中运行无异常,打包到D:\mapModule.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)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
[最优解释]
file:\D:\MapModule.jar!\img\map\map.jpg
看这句话楼主应该去找找你获得路径的那段话,应该获得的是全路径。网上搜下怎么获得绝对路径,不要全路径。是不是应该这样才对:file:d:\img\map\map.jpg
[其他解释]
嗯,是啊,我也知道这个路径不对啊,这个程序肯定不能用绝对路径的,我听说打包之后的文件只能读取,不能写,也不知道怎么回事
[其他解释]