16 July 2009

Eclipse + SWT 開發一個英文單字練習工具,忙了半天,想說先在不同電腦中測試既有的功能,在 SWT Class -> JAR -> EXE 文章中提到過,怎麼讓 JAR 可以轉 EXE 並在不同電腦上執行,結果 ~ 封裝出來的 FAT-JAR 只能在本機上執行 …

把封裝好的 Jar 上傳到筆電會一直出現

Could not find the main class.

光看上面的錯誤訊息,找不到 main class?? 明明程式裡面就有 public static void main(String[] args),試過各種方法,程式碼 mark 掉,再慢慢找問題,試了一個下午問題還是存在,在 jar文件Could not find the main class解决办法 看到一線曙光,沒想到原因竟然是程式找不到 import 的套件,跟字面上理解的 main class 完全沒關係。

開始 -> 執行 -> cmd 下執行 java -jar TEST_fat.jar 後,才從錯誤訊息裡面發現,
原來是引用 JMF 套件的時候,並沒有在 Eclipse 裡面指定外部套件,cmd 下的錯誤訊息為:

C:\>java -jar TEST_fat.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/NoPlayerE
xception
Caused by: java.lang.ClassNotFoundException: javax.media.NoPlayerException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: vocabulary.TEST. Program will exit.

這樣就比直接點兩下執行 JAR 檔出現的 “could not find the main class” 清楚多了。

</span>

參考資料:

  1. SWT Class -> JAR -> EXE @ 蛙齋
<address>
  <a href="http://wazai.net/1255/swt-class-jar-exe" target="_blank">http://wazai.net/1255/swt-class-jar-exe</a>
</address>
  1. jar文件Could not find the main class解决办法 @ 朱紫空间
<address>
  <a href="http://yintech.iteye.com/blog/374842" target="_blank">http://yintech.iteye.com/blog/374842</a>
</address>