顯示具有 Eclipse 標籤的文章。 顯示所有文章
顯示具有 Eclipse 標籤的文章。 顯示所有文章

Eclipse 啟動發生 libjvm.dylib does not contain the JNI_CreateJavaVM symbol

Eclipse/SpringToolSuites4 啟動發生 libjvm.dylib does not contain the JNI_CreateJavaVM symbol

更新 openjdk 後,無法開啟 Eclipse/SpringToolSuites4,錯誤訊息如下 :

The JVM shared library "/Users/foo/Library/Java/JavaVirtualMachines/adopt-openjdk-11.0.12/Contents/Home/bin/../lib/server/libjvm.dylib" does not contain the JNI_CreateJavaVM symbol.

看起來應該是更新的 openjdk(adopt-openjdk-11.0.12)有問題,編輯 Eclipse/SpringToolSuites4 的 Info.plist,指定要執行的 Java 路徑(原先正常的 adopt-openjdk-11.0.11)即可。

<!-- Info.plist 搜尋 -vm 字串 --> 

<string>-vm</string><string>/Users/foo/Library/Java/JavaVirtualMachines/adopt-openjdk-11.0.11/Contents/Home/bin/java</string>

特殊註解:TODO、FIXME、XXX

最早接觸到這些特殊註解是在寫 Eclipse 的時候,有些自動產生的程式碼就會自己補上 //TODO,在 Tasks 分頁就能顯示出這些備註,點擊就直接跳到程式碼上相當方便。

實際上在 Preferences > Java > Compiler > Task Tags 下可以看到預設只支援 TODO、FIXME 和 XXX 這三種特殊註釋。

除了 TODO、FIXME 和 XXX 外,我也列出了我比較少用,但在其他地方有看到的特殊註釋:

  • TODO:知道要實作什麼功能,但還沒開始寫的程式碼。比如://TODO 接上 LDAP 認證
  • FIXME:發現比較少發生的 bug,但當下沒時間改。比如://FIXME 對方回傳結果缺乏 foo 欄位,會造成 NullPointerException。
  • XXX:很醜但可以正常運作的程式碼,我會先用這個註釋標註,未來有空回來優化。比如://XXX 目前用 iterator 的方式掃描集合,可改用 Stream 的方式。
  • 少用
    • NOTE:說明這段程式碼怎麼運作。因為等於在寫一般的單行註解,所以我沒用過。
    • HACK:這裡用非正規的方法實作功能(Android 開發常用 reflection 機制呼叫沒開放的功能)。因為跟 XXX 類似,所以我很少用。
    • BUG:這裡有 bug。因為跟 FIXME 類似,所以我很少用。

Eclipse 啟動發生 Caused by: java.lang.ClassNotFoundException: javax.annotation.PreDestroy

因為開發環境的 SpringToolSuite4 要求更新 Java,更新到 Java 11 之後,反而 SpringToolSuite3 就打不開了,啟動就跳出下圖:

Eclipse 無法啟動

按照指示查看 log,看起來是跟 Java 9 引入的 module 功能相關,造成找不到這個 PreDestroy 這個類別。


java.lang.NoClassDefFoundError: javax/annotation/PreDestroy
        at org.eclipse.e4.core.internal.di.InjectorImpl.disposed(InjectorImpl.java:426)
        at org.eclipse.e4.core.internal.di.Requestor.disposed(Requestor.java:154)
        at org.eclipse.e4.core.internal.contexts.ContextObjectSupplier$ContextInjectionListener.update(ContextObjectSupplier.java:78)
        at org.eclipse.e4.core.internal.contexts.TrackableComputationExt.update(TrackableComputationExt.java:111)
        at org.eclipse.e4.core.internal.contexts.TrackableComputationExt.handleInvalid(TrackableComputationExt.java:74)
        at org.eclipse.e4.core.internal.contexts.EclipseContext.dispose(EclipseContext.java:176)
        at org.eclipse.e4.core.internal.contexts.osgi.EclipseContextOSGi.dispose(EclipseContextOSGi.java:106)
        at org.eclipse.e4.core.internal.contexts.osgi.EclipseContextOSGi.bundleChanged(EclipseContextOSGi.java:139)
        at org.eclipse.osgi.internal.framework.BundleContextImpl.dispatchEvent(BundleContextImpl.java:903)
        at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
        at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:148)
        at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEventPrivileged(EquinoxEventPublisher.java:213)
        at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEvent(EquinoxEventPublisher.java:120)
        at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEvent(EquinoxEventPublisher.java:112)
        at org.eclipse.osgi.internal.framework.EquinoxContainerAdaptor.publishModuleEvent(EquinoxContainerAdaptor.java:156)
        at org.eclipse.osgi.container.Module.publishEvent(Module.java:476)
        at org.eclipse.osgi.container.Module.doStop(Module.java:634)
        at org.eclipse.osgi.container.Module.stop(Module.java:498)
        at org.eclipse.osgi.container.SystemModule.stop(SystemModule.java:202)
        at org.eclipse.osgi.internal.framework.EquinoxBundle$SystemBundle$EquinoxSystemModule$1.run(EquinoxBundle.java:165)
        at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ClassNotFoundException: javax.annotation.PreDestroy cannot be found by org.eclipse.e4.core.di_1.6.1.v20160712-0927
        at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:410)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:372)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:364)
        at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:161)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 21 more

解決方法:以下方法為 Eclipse / SpringToolSuite 通用,到 Eclipse / SpringToolSuite 資料夾下編輯 eclipse.ini 或 STS.ini,在 -vmargs 之前加上 -vm 參數指定啟動使用的 Java,下方範例我指定到我原本安裝的 Java 8 其 bin 資料夾下的 java 檔案。

另外要注意,-vm 是一列,指定路徑也是單獨一列,記得換行!


... 忽略
-vm
/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/java
-vmargs
... 忽略

Spark - Java 輕量級的 Web 框架

這跟處理巨量資料的 apache spark 無關!

如果有時候只是要寫個沒幾個人用的 Restful API,或只是想要有個 HTTP 接口(透過 get/post)來呼叫我們寫好的 Java SE 程式,如果對架設 tomcat / Java EE Server 又不熟悉,那可以考慮用 Spark!

Hello World

Spark 2 以後需要用到 lambda 語法,需要安裝 Java 8。如果只有 Java 7,請找 Spark 1 或是去玩 play

以 Eclipse 為例,安裝 Spark 方法有兩種:

第一種比較簡單,直接建立 Maven 專案,在 pom.xml 加入對 spark 的 dependency 就好了。

第二種就到 github clone 下來,不過實際建立時,我還是得把這專案轉成 maven 專案,才能利用 pom.xml 把其他要用的 jar 檔抓回來...也可能我太笨沒抓到官方給的提示...

寫一個 HelloWorld.java 包含一個 main 如下:

import static spark.Spark.*;

public class HelloWorld {
    public static void main(String[] args) {
        get("/hello", (req, res) -> "Hello World");
    }
}

直接執行 HelloWorld.java,無需安裝另外的 Web Container,無需任何設定,在瀏覽器打開 http://localhost:4567/hello 就可以看到結果。

螢幕快照 2015-03-26 下午1.49.01

Spark 提供的功能

大部份 Restful API 需要的功能都有:
  • 指定 URL 路徑。
  • 存取 request 標頭/參數/...。
  • 回寫 response。
  • 處理 session。
  • 處理每個 request 前後插入 filter 做前置/後置處理。
除了單純回傳資料,也可以介接 Freemarker 這類的 HTML Template Engine,將要顯示的值以類似 JSP EL 的方式,填到預先寫好的 HTML Template 上。

也可以介接 AngularJS 做出類似 Gmail 這種只有一頁的 Web 網站。

部署

官網上沒有特別寫部屬的方式,試了一下。就直接在 Eclipse 上匯出一個可執行的 jar,執行的目標就指向有 main 的那支 java。

執行匯出的 jar 就會在本機上開啟 Web Server 了(參考下圖)!開發到使用完全就像執行一般 jar 一樣!

螢幕快照 2015-03-26 下午1.51.17

參考

Eclipse 產生 Web Service Client 問題排除

網路上 step by step 的文章已經很多了,這邊僅記錄我在 Eclipse + JBoss7 上產生 Client 遇到的問題。不定期更新(如果我還持續踩到雷的話)。

不支援 static array

編輯 WSDL 時,Eclipse 會回報以下錯誤。
Multiple annotations found at this line:
    - WS-I: (BP2108) An Array declaration uses - restricts or extends - the soapenc:Array type, or the 
     wsdl:arrayType attribute is used in the type declaration.
    - WS-I: (BP2102) An XML schema import element references something other than an XML 
     schema definition: the referenced element does not have "schema" from XML namespace "http://
     www.w3.org/2001/XMLSchema" as root element.
修改 WSDL ,改為 dynamic array style。

參考:

不支援 RPC / Encoded

編輯 WSDL 時,Eclipse 會回報以下錯誤。
WS-I: (BP2406) The use attribute of a soapbind:body, soapbind:fault, soapbind:header and soapbind:headerfault does not have value of "literal".

修改 WSDL ,將 encoded 改為 literal。雖然這樣修改能順利產生 Web Service Client 的程式碼,但最終還是要看 Web Service 的 Server 端能不能接受 literal 的 SOAP。
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace=http://foo/bar use="encoded"/>

<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace=http://foo/bar use="literal"/>

Eclipse 執行完成卻沒有產生任何程式碼

沒有產生程式碼,沒有任何報錯。

用 console 模式來產生程式碼,以 JBoss 7 為例,直接使用 ${jboss_home}/bin/wsconsume.bat 來產生程式碼,錯誤就能正確顯示。



參考:

惱人的 CreateProcess error=206


問題

執行 JUnit 時會吐出以下類似錯誤訊息:
Exception occurred executing command line.Cannot run program "C:\Program Files\Java\jre6\bin\javaw.exe" (in directory "D:\src\expweb\expweb\trunk"): CreateProcess error=206, The filename or extension is too long


解法

Someone has written the SOP, you can fix this bug on your own.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=327193#c69
有人幫忙修改好相關的程式碼轉成 class 檔,下載後按下面步驟做即可:
  1. 備份 ${elipse_home}/plugins/org.eclipse.jdt.launching_3.4.*.jar。
  2. 關掉 Eclipse。
  3. 把 ${elipse_home}/plugins/org.eclipse.jdt.launching_3.4.*.jar 改成 .zip。
  4. 把下載的四個 class 檔案放入 zip 檔中的 org\eclipse\jdt\internal\launching (覆蓋原有的 class 檔)。
  5. 把 zip 檔 META-INF 資料夾下除了 MANIFEST.MF 都刪除。
  6. 修改 MANIFEST.MF 把 "NAME:" 開頭的項目都刪除,確認有留兩列空白列。
  7. 把 .zip 改回 .jar。
  8. 重啟 Eclipse 即可。


下載 Download Here!!

If you are using Eclipse 4.2 Juno and org.eclipse.jdt.launching_3.6.100.v20120523-1953.jar, you can download the new jar file here! Enjoy it!

如果你剛好跟我一樣
  • 都是用 Eclipse 4.2 Juno
  • jar 檔名也是 org.eclipse.jdt.launching_3.6.100.v20120523-1953.jar
我已經修改包好,你可以到 GoogleDrive 下載。


官方討論

https://bugs.eclipse.org/bugs/show_bug.cgi?id=327193
看來是因為引用到的外部函示庫、其他專案太多,造成 Eclipse 呼叫 Java 執行 JUnit 時的 ClassPath 太長就爆炸了。

在 Windows 下的 Eclipse 都有可能發生此問題,然後官方只會在 Eclipse 4.3 之後才會修復(官方認為大多數人都不會發生這問題,但也不是每個人都只是寫寫 Hello World 啊 囧rz)。

Eclipse 常用 Plug-in

最近電腦爆炸換系統重新建立環境.

特以此篇紀錄我 Eclipse 下常用的套件,讓未來的我能快速知道該下載些什麼及到哪下載!

  • JInto - 多國語言訊息編輯.
  • SQL Explorer - 查詢資料庫.
  • Regex Util - 正規表達式.
  • Mylyn - 工作進度系統,( Eclipse 3.4 已內建).
  • Jigloo - 快速建立 GUI .
  • TestNG - 測試程式. 雖然 Ivan 不在了! 但他說的話猶存於耳! 我時間允許的話還是會先寫測試程式 Orz