Java 代码在eclipse中编译时可以工作,但不能作为可运行的jar
下面是有问题的代码Java 代码在eclipse中编译时可以工作,但不能作为可运行的jar,java,eclipse,Java,Eclipse,下面是有问题的代码 static void load() { ObjectInputStream ois = null; try { ois = new ObjectInputStream(ClassLoader.getSystemClassLoader().getResourceAsStream("resources/handMap.ser")); if (ois == null)
static void load() {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(ClassLoader.getSystemClassLoader().getResourceAsStream("resources/handMap.ser"));
if (ois == null)
JOptionPane.showMessageDialog(null, "null file");
handMap = (HashMap<Long, Integer>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
静态空荷载(){
ObjectInputStream ois=null;
试一试{
ois=newObjectInputStream(ClassLoader.getSystemClassLoader().getResourceAsStream(“resources/mandp.ser”);
如果(ois==null)
showMessageDialog(null,“null文件”);
手工映射=(HashMap)ois.readObject();
}捕获(IOException | ClassNotFoundException e){
showMessageDialog(null,e.getMessage());
}
试一试{
ois.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
它应该做的是将一个序列化的HashMap加载到内存中。我的文件位于程序的src文件夹下的resources文件夹中。当我通过eclipse运行它时,似乎没有问题。但是,当我将其作为可运行jar运行时,它似乎无法正确加载文件。我已经做了一些jdialogs来让我了解代码哪里出了问题,而且看起来执行永远不会超过应该将文件加载到ObjectInputStream中的部分。你知道哪里出了问题吗?正如我提到的,该程序在Eclipse中运行得非常好。您是否在最后一次正确的更改后重新打包了您的jar?请使用
classpath:maidp.ser
而不是“resources/maidp.ser”
尝试“/maidp.ser”
删除资源感谢大家的帮助。问题似乎是文件将其放在资源文件夹中,而不是直接放在src文件夹下。