GraalVM tips in Windows

Generate Native Image

Prerequisites

  • Install VS2017+ in your Windows laptop. (Too easy in Linux and Mac)
    If you are not installing it properly (E.G. Just copied Windows Kits and the Microsoft Visual Studio folder), you might lost some environment variable. Please remember to run Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat as the x64 Native Tools Command Prompt
    You might also want to change the take care about these variables:
    • INCLUDE (Check it from some other machine with the x64 Native Tools Command Prompt by echo %INCLUDE%)
    • LIB (Ditto)
set INCLUDE=<Copy from another good machine>
set LIB=<Copy from another good machine>

Sometimes the Anti-Virus tool will block you from creating files at C:\Users\<user>\AppData\Local\Temp. To make your life easier, you might change the TEMP and TMP variable by:

set TMP=c:\Temp
set TEMP=c:\Temp
  • Install the GraalVM and Native-image tool for Windows.

For Swing / Awt related application. You might encounter below errors. You can avoid them by the Step-by-steps section.

# Error 1:
Exception in thread "AWT-EventQueue-0" java.lang.Error: java.home property not set


# Error 2: 
xception in thread "main" java.lang.UnsatisfiedLinkError: no awt in java.library.path
        at org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk.NativeLibrarySupport.loadLibraryRelative(NativeLibrarySupport.java:120)

Step-by-steps

  • Firstly, you need to generate the agent trace files
    • Use $GRAAL_HOME\bin\java to run your jar (do not do it with IDE run directly because will add a lot of IDE related stuff and fail your build later on), add below JVM options
      -agentlib:native-image-agent=config-output-dir=<project-base>\src\main\resources\META-INF\native-image\<your group>\<your artifact>
      
    • Run all the app with all use cases.
    • Close the app.
    • Use mvn to repackage the app. (Ensure the generated META-INF files are included to your package)
  • Start the x64 Native Tools Command Prompt for VS 2022
  • Run native-image command to build the jar created by maven.
  # -O0 for testing purpose, it will not optimize the exe
  > native-image -O0 -o my-exe-file -jar <project base>\target\my-jar.jar --initialize-at-build-time=org.slf4j.LoggerFactory -H:+ReportExceptionStackTraces
  • Each time failed, you can adjust the --initialize-at-build-time and --initialize-at-run-time to remove some errors. For example, you might encounter a lot errors like below. (You should read the error message carefully, with the - --trace-class-initialization=<track the class>, you will know which Class to be added to the initialize at run/build time.) (Example https://stackoverflow.com/questions/63328298/)
SEVERE: Unable to initialize BouncyCastleAlpnSslUtils.
java.lang.ClassNotFoundException: org.bouncycastle.jsse.BCSSLEngine

Error: No instances of java.net.Inet4Address are allowed in the image heap as this class should be initialized at image runtime. Object has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked.
Trace: Object was reached by ...

Error: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: No instances of java.net.Inet4Address are allowed in the image heap as this class should be initialized at image runtime. Object has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked.
  • After tested okay, use the optimized parameter to re-compile it.
  # Now optimize it 
  > native-image -H:Name=my-exe-file -jar <project base>\target\my-jar.jar --initialize-at-build-time=org.slf4j.LoggerFactory -H:+ReportExceptionStackTraces -o my-exe-file -O3
  • You need to include some lib files in JDK to avoid awt issue.
> dir lib
2022/11/24  17:58    <DIR>          .
2022/11/24  17:58    <DIR>          ..
2022/10/20  12:43             4,630 fontconfig.bfc
2022/10/20  12:43            12,345 fontconfig.properties.src
2022/10/20  12:43             3,793 psfont.properties.ja
2022/10/20  12:43            11,390 psfontj2d.properties
  • Then run command: bin/my-exe.exe -Djava.home=<the parent folder path> (The java home is very simple to contain the lib folder and the 4 font related files in it)
-- parent folder
  |-- bin (The files inside is generated by the graal-vm automatically)
       |-- my-exe.exe
       |-- jvm.dll
       |-- awt.dll
       |-- ... (some other dll files)
  |-- lib (this one nees to be manually prepared)
       |-- fontconfig.bfc
       |-- fontconfig.properties.src
       |-- psfont.properties.ja
       |__ psfontj2d.properties
  • To remove the console, run below command in the x64 Native Tools Command Prompt
    EDITBIN /SUBSYSTEM:WINDOWS my-exe.exe
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末狂塘,一起剝皮案震驚了整個(gè)濱河市鳄厌,隨后出現(xiàn)的幾起案子荞胡,更是在濱河造成了極大的恐慌了嚎,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件萝勤,死亡現(xiàn)場(chǎng)離奇詭異呐伞,居然都是意外死亡敌卓,警方通過(guò)查閱死者的電腦和手機(jī)荸哟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)鞍历,“玉大人,你說(shuō)我怎么就攤上這事劣砍。” “怎么了刑枝?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵装畅,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我掠兄,道長(zhǎng),這世上最難降的妖魔是什么蚂夕? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任婿牍,我火速辦了婚禮,結(jié)果婚禮上等脂,老公的妹妹穿的比我還像新娘撑蚌。我一直安慰自己嫁蛇,他們只是感情好锨并,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布睬棚。 她就那樣靜靜地躺著,像睡著了一般包警。 火紅的嫁衣襯著肌膚如雪底靠。 梳的紋絲不亂的頭發(fā)上害晦,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天壹瘟,我揣著相機(jī)與錄音鳄逾,去河邊找鬼稻轨。 笑死雕凹,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的枚抵。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼李丰,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼逼泣!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起圾旨,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤砍的,失蹤者是張志新(化名)和其女友劉穎莺治,沒想到半個(gè)月后廓鞠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡滋早,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年砌们,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片浪感。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡影兽,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出峻堰,到底是詐尸還是另有隱情,我是刑警寧澤捐名,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站纸型,受9級(jí)特大地震影響梅忌,放射性物質(zhì)發(fā)生泄漏狰腌。R本人自食惡果不足惜牧氮,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望丹莲。 院中可真熱鬧尸诽,春花似錦甥材、人聲如沸性含。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至苛谷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間腹殿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工绵患, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留悟耘,地道東北人落蝙。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓筏勒,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親管行。 傳聞我的和親對(duì)象是個(gè)殘疾皇子邪媳,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容