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() 來獲取圖片,就不會發生因路徑變化找不到圖片的問題。