1. 错误1:打 jar 包执行,报错,找不到 类库的 jar 包
F:>java -jar remoteLogin.jar
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/alibaba/fastjson/JSON
at com.diantusoft.LoginWindow.getFingerPrintFromDB(LoginWindow.java:819)
at com.diantusoft.LoginWindow.login(LoginWindow.java:630)
at com.diantusoft.LoginWindow.access$8(LoginWindow.java:577)
at com.diantusoft.LoginWindow$13.mouseClicked(LoginWindow.java:509)
at java.awt.Component.processMouseEvent(Component.java:6528)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: com.alibaba.fastjson.JSON
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
解决方法:Use Export -> Java -> Runnable JAR File instead Export -> Java -> JAR File on your project (right mouse click).
2. 错误2: Could not find main method from given launch configuration
原因:选错了 launch configuration,自然就找不到 main方法,选择时要选择那个包括 main 方法的 类作为 launch
3. 错误3:
public static ImageIcon getPicture2(String name) { ImageIcon icon = new ImageIcon(PictureUtil.class.getClassLoader() .getResource("com/diantusoft/image/" + name)); return icon; }
如下代码,eclipse export 导出 jar 包之后,执行 无法找到路径:
因为路径变成了如下的形式:file:/F:/remoteLogin.jar!/com/diantusoft/image/login_button.png
所以 java -jar remoteLogin.jar 执行时,是如法找到图片的。
解决方法:
private static PictureUtil instance = new PictureUtil(); private PictureUtil(){} public static PictureUtil getInstance(){ return instance; } public ImageIcon getPicture(String name){ URL url = this.getClass().getResource(name); if(url != null){ return new ImageIcon(url); } }
也就是使用 this.getClass().getResource() 来获取图片,就不会发生因路径变化找不到图片的问题。