Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native-image] Swing application cannot be compiled on Windows #3659

Closed
BullyWiiPlaza opened this issue Aug 8, 2021 · 20 comments
Closed

[native-image] Swing application cannot be compiled on Windows #3659

BullyWiiPlaza opened this issue Aug 8, 2021 · 20 comments
Assignees

Comments

@BullyWiiPlaza
Copy link

Describe the issue
When compiling my simple hello world Swing application on Windows using native-image, it does not work.

Steps to reproduce the issue

  1. Write a simple Swing application on Windows
  2. Compile it into a executable JAR
  3. Verify the executable JAR works as intended
  4. Use native-image to compile the JAR into an EXE using e.g. native-image -jar "Hello World.jar" "Hello World"
  5. An exception occurs: Fatal error:com.oracle.svm.core.util.VMError$HostedError: should not reach here

Describe GraalVM and your environment:

More details

>native-image -jar "Hello World.jar" "Hello World" --verbose
Executing [
'C:\graalvm_jdk16\bin\java.exe' \
-XX:+UseParallelGC \
-XX:+UnlockExperimentalVMOptions \
-XX:+EnableJVMCI \
-Dtruffle.TrustAllTruffleRuntimeProviders=true \
-Dtruffle.TruffleRuntime=com.oracle.truffle.api.impl.DefaultTruffleRuntime \
-Dgraalvm.ForcePolyglotInvalid=true \
-Dgraalvm.locatorDisabled=true \
-Dsubstratevm.IgnoreGraalVersionCheck=true \
--add-exports=java.base/com.sun.crypto.provider=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.access.foreign=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.event=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.loader=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.logger=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.module=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.org.objectweb.asm=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.org.xml.sax.helpers=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.perf=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.ref=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.util.xml.impl=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.util.xml=ALL-UNNAMED \
--add-exports=java.base/sun.invoke.util=ALL-UNNAMED \
--add-exports=java.base/sun.nio.ch=ALL-UNNAMED \
--add-exports=java.base/sun.reflect.annotation=ALL-UNNAMED \
--add-exports=java.base/sun.reflect.generics.reflectiveObjects=ALL-UNNAMED \
--add-exports=java.base/sun.reflect.generics.repository=ALL-UNNAMED \
--add-exports=java.base/sun.reflect.generics.tree=ALL-UNNAMED \
--add-exports=java.base/sun.security.jca=ALL-UNNAMED \
--add-exports=java.base/sun.security.provider=ALL-UNNAMED \
--add-exports=java.base/sun.security.util=ALL-UNNAMED \
--add-exports=java.base/sun.text.spi=ALL-UNNAMED \
--add-exports=java.base/sun.util.calendar=ALL-UNNAMED \
--add-exports=java.base/sun.util.locale.provider=ALL-UNNAMED \
--add-exports=java.base/sun.util.resources=ALL-UNNAMED \
--add-exports=java.xml.crypto/org.jcp.xml.dsig.internal.dom=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.aarch64=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.amd64=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.code.site=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.code.stack=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.code=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.common=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.hotspot.aarch64=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.hotspot.amd64=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.hotspot=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.meta=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.runtime=ALL-UNNAMED \
--add-exports=jdk.internal.vm.ci/jdk.vm.ci.services=ALL-UNNAMED \
--add-exports=jdk.jfr/jdk.jfr.events=ALL-UNNAMED \
--add-exports=jdk.jfr/jdk.jfr.internal.consumer=ALL-UNNAMED \
--add-exports=jdk.jfr/jdk.jfr.internal.handlers=ALL-UNNAMED \
--add-exports=jdk.jfr/jdk.jfr.internal.jfc=ALL-UNNAMED \
--add-exports=jdk.jfr/jdk.jfr.internal=ALL-UNNAMED \
-XX:+UseJVMCINativeLibrary \
-Xss10m \
-Xms1g \
-Xmx14g \
-Duser.country=US \
-Duser.language=en \
-Djava.awt.headless=true \
-Dorg.graalvm.version=21.3.0-dev \
-Dorg.graalvm.config=CE \
-Dcom.oracle.graalvm.isaot=true \
-Djava.system.class.loader=com.oracle.svm.hosted.NativeImageSystemClassLoader \
-Xshare:off \
--module-path \
'C:\graalvm_jdk16\lib\truffle\truffle-api.jar' \
-Djdk.internal.lambda.disableEagerInitialization=true \
-Djdk.internal.lambda.eagerlyInitialize=false \
-Djava.lang.invoke.InnerClassLambdaMetafactory.initializeLambdas=false \
'-javaagent:C:\graalvm_jdk16\lib\svm\builder\svm.jar' \
-cp \
'C:\graalvm_jdk16\lib\svm\builder\objectfile.jar;C:\graalvm_jdk16\lib\svm\builder\pointsto.jar;C:\graalvm_jdk16\lib\svm\builder\svm.jar' \
'com.oracle.svm.hosted.NativeImageGeneratorRunner$JDK9Plus' \
-imagecp \
'D:/JavaProjects/Hello World GraalVM\Hello World.jar;C:\graalvm_jdk16\lib\svm\library-support.jar' \
'-H:Path=D:/JavaProjects/Hello World GraalVM' \
'-H:Class@manifest from file:///D:/JavaProjects/Hello%20World%20GraalVM/Hello%20World.jar=Main' \
'-H:Name@manifest from file:///D:/JavaProjects/Hello%20World%20GraalVM/Hello%20World.jar=Hello World' \
'-H:CLibraryPath=C:\graalvm_jdk16\lib\svm\clibraries\windows-amd64' \
'-H:Name@explicit image name=Hello World'
]
[Hello World:21804]    classlist:   1,425.84 ms,  0.96 GB
[Hello World:21804]        (cap):   1,848.14 ms,  0.96 GB
[Hello World:21804]        setup:   3,349.53 ms,  0.96 GB
[Hello World:21804]     (clinit):     466.52 ms,  3.79 GB
[Hello World:21804]   (typeflow):  10,202.45 ms,  3.79 GB
[Hello World:21804]    (objects):   8,178.93 ms,  3.79 GB
[Hello World:21804]   (features):     900.09 ms,  3.79 GB
[Hello World:21804]     analysis:  21,964.79 ms,  3.79 GB
[Hello World:21804]     universe:   1,774.63 ms,  3.79 GB
[Hello World:21804]      (parse):   1,473.85 ms,  3.80 GB
[Hello World:21804]     (inline):   1,953.48 ms,  4.24 GB
[Hello World:21804]    (compile):  23,004.54 ms,  4.86 GB
[Hello World:21804]      compile:  27,992.96 ms,  4.86 GB
[Hello World:21804]        image:   2,549.32 ms,  4.86 GB
[Hello World:21804]        write:   1,058.40 ms,  4.86 GB
Fatal error:com.oracle.svm.core.util.VMError$HostedError: should not reach here
        at com.oracle.svm.core.util.VMError.shouldNotReachHere(VMError.java:64)
        at com.oracle.svm.hosted.jdk.JNIRegistrationSupport.makeShimDLL(JNIRegistrationSupport.java:256)
        at com.oracle.svm.hosted.jdk.JNIRegistrationSupport.makeShimDLLs(JNIRegistrationSupport.java:230)
        at com.oracle.svm.hosted.jdk.JNIRegistrationSupport.afterImageWrite(JNIRegistrationSupport.java:175)
        at com.oracle.svm.hosted.NativeImageGenerator.lambda$doRun$8(NativeImageGenerator.java:667)
        at com.oracle.svm.hosted.FeatureHandler.forEachFeature(FeatureHandler.java:73)
        at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:667)
        at com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:492)
        at com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:400)
        at com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:566)
        at com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:122)
        at com.oracle.svm.hosted.NativeImageGeneratorRunner$JDK9Plus.main(NativeImageGeneratorRunner.java:596)
[Hello World:21804]      [total]:  60,342.13 ms,  4.86 GB
# Printing build artifacts to: D:\JavaProjects\Hello World GraalVM\native\Hello World.build_artifacts.txt
Error: Image build request failed with exit status 1
com.oracle.svm.driver.NativeImage$NativeImageError: Image build request failed with exit status 1
        at com.oracle.svm.driver.NativeImage.showError(NativeImage.java:1867)
        at com.oracle.svm.driver.NativeImage.build(NativeImage.java:1512)
        at com.oracle.svm.driver.NativeImage.performBuild(NativeImage.java:1473)
        at com.oracle.svm.driver.NativeImage.main(NativeImage.java:1460)

What's the current state of Swing support for Windows? Since Windows is the most popular desktop OS, I don't understand why apparently only Linux support has been implemented. Is there any command line switch or similar I can try to potentially make it work for Windows right now?

Related issue: #1327

@chirontt
Copy link

chirontt commented Aug 9, 2021

This is similar to #3575
You must run your Swing app with the native-image-agent to generate the config files for the native-image command to use.
Also, try using the GraalVM version for JDK11, as the JDK16 version may be problematic (it's experimental status, after all)

@munishchouhan
Copy link
Contributor

@BullyWiiPlaza as suggested by @chirontt
please use native-image tracing agent
https://docs.oracle.com/en/graalvm/enterprise/21/docs/reference-manual/native-image/Agent/
and GraalVM-21.2-Java11

@munishchouhan munishchouhan self-assigned this Aug 12, 2021
@BullyWiiPlaza
Copy link
Author

I used the Java 11 version of GraalVM before but got the same error using the Java 16 version so I thought the latest version has the best chances to work.

However, now I setup a new project with similar code and the native-image compilation actually succeeded. Though, upon running the EXE it crashed and I got the following exception:

Exception in thread "main" java.lang.NoSuchMethodError: java.awt.Toolkit.getDefaultToolkit()Ljava/awt/Toolkit;
        at com.oracle.svm.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1104)
        at com.oracle.svm.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1089)
        at com.oracle.svm.jni.functions.JNIFunctions.GetStaticMethodID(JNIFunctions.java:416)
        at java.awt.Toolkit.initIDs(Toolkit.java)
        at java.awt.Toolkit.<clinit>(Toolkit.java:1427)
        at com.oracle.svm.core.classinitialization.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:375)
        at com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:295)
        at javax.swing.UIManager.<clinit>(UIManager.java:383)
        at com.oracle.svm.core.classinitialization.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:375)
        at com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:295)
        at Main.main(Main.java:9)

After tracing all API usages using the tracing agent and adding the configs to the META_INF/native-image directory inside my JAR, I got the following different exception(s) upon running:

Exception in thread "AWT-EventQueue-0" java.lang.InternalError: java.lang.reflect.InvocationTargetException
        at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:86)
        at java.security.AccessController.doPrivileged(AccessController.java:87)
        at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
        at java.awt.Font.getFont2D(Font.java:497)
        at java.awt.Font$FontAccessImpl.getFont2D(Font.java:237)
        at sun.font.FontUtilities.getFont2D(FontUtilities.java:158)
        at sun.font.FontUtilities.fontSupportsDefaultEncoding(FontUtilities.java:338)
        at com.sun.java.swing.plaf.windows.WindowsLookAndFeel$WindowsFontProperty.configureValue(WindowsLookAndFeel.java:2240)
        at sun.swing.plaf.DesktopProperty.createValue(DesktopProperty.java:159)
        at javax.swing.UIDefaults.getFromHashtable(UIDefaults.java:239)
        at javax.swing.UIDefaults.get(UIDefaults.java:169)
        at javax.swing.MultiUIDefaults.get(MultiUIDefaults.java:65)
        at javax.swing.UIDefaults.getFont(UIDefaults.java:419)
        at javax.swing.UIManager.getFont(UIManager.java:722)
        at javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:209)
        at javax.swing.plaf.basic.BasicPanelUI.installDefaults(BasicPanelUI.java:77)
        at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:62)
        at javax.swing.JComponent.setUI(JComponent.java:685)
        at javax.swing.JPanel.setUI(JPanel.java:150)
        at javax.swing.JPanel.updateUI(JPanel.java:126)
        at javax.swing.JPanel.<init>(JPanel.java:86)
        at javax.swing.JPanel.<init>(JPanel.java:109)
        at javax.swing.JPanel.<init>(JPanel.java:117)
        at javax.swing.JRootPane.createGlassPane(JRootPane.java:521)
        at javax.swing.JRootPane.<init>(JRootPane.java:348)
        at javax.swing.JFrame.createRootPane(JFrame.java:279)
        at javax.swing.JFrame.frameInit(JFrame.java:258)
        at javax.swing.JFrame.<init>(JFrame.java:181)
        at PrimeNumbersCalculatorGUI.<init>(PrimeNumbersCalculatorGUI.java:10)
        at Main.lambda$main$0(Main.java:13)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
        at java.awt.EventQueue$4.run(EventQueue.java:721)
        at java.awt.EventQueue$4.run(EventQueue.java:715)
        at java.security.AccessController.doPrivileged(AccessController.java:105)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
        at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:567)
        at com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine(WindowsJavaThreads.java:138)
Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Constructor.java:490)
        at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:84)
        ... 44 more
Caused by: java.lang.Error: java.home property not set
        at sun.awt.FontConfiguration.findFontConfigFile(FontConfiguration.java:182)
        at sun.awt.FontConfiguration.<init>(FontConfiguration.java:99)
        at sun.awt.windows.WFontConfiguration.<init>(WFontConfiguration.java:41)
        at sun.awt.Win32FontManager.createFontConfiguration(Win32FontManager.java:179)
        at sun.font.SunFontManager$2.run(SunFontManager.java:379)
        at java.security.AccessController.doPrivileged(AccessController.java:87)
        at sun.font.SunFontManager.<init>(SunFontManager.java:324)
        at sun.awt.Win32FontManager.<init>(Win32FontManager.java:87)
        ... 46 more

Looks like this is related to #1812 which is still an open issue so there does not seem to be a fix yet.

@munishchouhan
Copy link
Contributor

@BullyWiiPlaza please try GraalVM 21.3-dev
https://github.com/graalvm/graalvm-ce-dev-builds/releases/tag/21.3.0-dev-20210813_0049
and let us know the output

@chirontt
Copy link

@BullyWiiPlaza, for your java.lang.Error: java.home property not set error, which only happens in Windows native image, there are many ways to work around it. The simplest way is to invoke your native executable with the java.home system property pointing to your JDK, i.e.

your-app.exe -Djava.home=%JAVA_HOME%

@BullyWiiPlaza
Copy link
Author

@BullyWiiPlaza please try GraalVM 21.3-dev
https://github.com/graalvm/graalvm-ce-dev-builds/releases/tag/21.3.0-dev-20210813_0049
and let us know the output

I downloaded https://github.com/graalvm/graalvm-ce-dev-builds/releases/download/21.3.0-dev-20210813_0049/graalvm-ce-java11-windows-amd64-dev.zip and used that version now (see results below).

@BullyWiiPlaza, for your java.lang.Error: java.home property not set error, which only happens in Windows native image, there are many ways to work around it. The simplest way is to invoke your native executable with the java.home system property pointing to your JDK, i.e.

your-app.exe -Djava.home=%JAVA_HOME%

>Prime-Numbers-Calculator.exe -Djava.home=%JAVA_HOME%
Exception in thread "AWT-EventQueue-0" java.lang.InternalError: java.lang.reflect.InvocationTargetException
        at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:86)
        at java.security.AccessController.doPrivileged(AccessController.java:87)
        at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
        at java.awt.Font.getFont2D(Font.java:497)
        at java.awt.Font$FontAccessImpl.getFont2D(Font.java:237)
        at sun.font.FontUtilities.getFont2D(FontUtilities.java:158)
        at sun.font.FontUtilities.fontSupportsDefaultEncoding(FontUtilities.java:338)
        at com.sun.java.swing.plaf.windows.WindowsLookAndFeel$WindowsFontProperty.configureValue(WindowsLookAndFeel.java:2240)
        at sun.swing.plaf.DesktopProperty.createValue(DesktopProperty.java:159)
        at javax.swing.UIDefaults.getFromHashtable(UIDefaults.java:239)
        at javax.swing.UIDefaults.get(UIDefaults.java:169)
        at javax.swing.MultiUIDefaults.get(MultiUIDefaults.java:65)
        at javax.swing.UIDefaults.getFont(UIDefaults.java:419)
        at javax.swing.UIManager.getFont(UIManager.java:722)
        at javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:209)
        at javax.swing.plaf.basic.BasicPanelUI.installDefaults(BasicPanelUI.java:77)
        at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:62)
        at javax.swing.JComponent.setUI(JComponent.java:685)
        at javax.swing.JPanel.setUI(JPanel.java:150)
        at javax.swing.JPanel.updateUI(JPanel.java:126)
        at javax.swing.JPanel.<init>(JPanel.java:86)
        at javax.swing.JPanel.<init>(JPanel.java:109)
        at javax.swing.JPanel.<init>(JPanel.java:117)
        at javax.swing.JRootPane.createGlassPane(JRootPane.java:521)
        at javax.swing.JRootPane.<init>(JRootPane.java:348)
        at javax.swing.JFrame.createRootPane(JFrame.java:279)
        at javax.swing.JFrame.frameInit(JFrame.java:258)
        at javax.swing.JFrame.<init>(JFrame.java:181)
        at PrimeNumbersCalculatorGUI.<init>(PrimeNumbersCalculatorGUI.java:10)
        at Main.lambda$main$0(Main.java:13)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
        at java.awt.EventQueue$4.run(EventQueue.java:721)
        at java.awt.EventQueue$4.run(EventQueue.java:715)
        at java.security.AccessController.doPrivileged(AccessController.java:105)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
        at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:567)
        at com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine(WindowsJavaThreads.java:138)
Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Constructor.java:490)
        at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:84)
        ... 44 more
Caused by: java.lang.NullPointerException
        at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1262)
        at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:225)
        at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
        at sun.awt.Win32FontManager.createFontConfiguration(Win32FontManager.java:180)
        at sun.font.SunFontManager$2.run(SunFontManager.java:379)
        at java.security.AccessController.doPrivileged(AccessController.java:87)
        at sun.font.SunFontManager.<init>(SunFontManager.java:324)
        at sun.awt.Win32FontManager.<init>(Win32FontManager.java:87)
        ... 46 more

It gives a NullPointerException instead now. My JAVA_HOME is set of course:

>echo %JAVA_HOME%
C:\Program Files\Java\jdk-11.0.7

@munishchouhan
Copy link
Contributor

munishchouhan commented Aug 19, 2021

@BullyWiiPlaza
please check this issue
#3393
there is a solution for InvocationTargetException

@chirontt
Copy link

@BullyWiiPlaza
The java.lang.NullPointerException is caused by the space character in your JAVA_HOME setting!
You should escape it with double-quotes:
>Prime-Numbers-Calculator.exe -Djava.home="%JAVA_HOME%"

@BullyWiiPlaza
Copy link
Author

BullyWiiPlaza commented Aug 19, 2021

@BullyWiiPlaza
The java.lang.NullPointerException is caused by the space character in your JAVA_HOME setting!
You should escape it with double-quotes:
>Prime-Numbers-Calculator.exe -Djava.home="%JAVA_HOME%"

Wow, now it works and the GUI loads up, thanks for the patience.
However, when I drag the GUI around in my other monitor it throws the following exception repeatedly:

Exception in thread "AWT-Windows": java.lang.NoSuchMethodError
java.lang.NoSuchMethodError: sun.awt.windows.WFramePeer.draggedToNewScreen()V
        at com.oracle.svm.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1114)
        at com.oracle.svm.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1099)
        at com.oracle.svm.jni.functions.JNIFunctions.GetMethodID(JNIFunctions.java:410)
        at sun.awt.windows.WToolkit.eventLoop(WToolkit.java)
        at sun.awt.windows.WToolkit.run(WToolkit.java:305)
        at java.lang.Thread.run(Thread.java:829)
        at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:567)
        at com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine(WindowsJavaThreads.java:138)

This seems like something Oracle has forgotten to handle properly.

Also the following exception came up after clicking the "Calculate" button for prime numbers:

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: java.awt.Point.<init>(II)V
        at com.oracle.svm.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1114)
        at com.oracle.svm.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1099)
        at com.oracle.svm.jni.functions.JNIFunctions.GetMethodID(JNIFunctions.java:410)
        at sun.awt.windows.WComponentPeer.getLocationOnScreen(WComponentPeer.java)
        at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2090)
        at java.awt.Component.getLocationOnScreen(Component.java:2075)
        at java.awt.LightweightDispatcher.eventDispatched(Container.java:4799)
        at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Toolkit.java:2194)
        at java.awt.Toolkit.notifyAWTEventListeners(Toolkit.java:2042)
        at java.awt.Component.dispatchEventImpl(Component.java:4917)
        at java.awt.Container.dispatchEventImpl(Container.java:2321)
        at java.awt.Component.dispatchEvent(Component.java:4843)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4564)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
        at java.awt.Container.dispatchEventImpl(Container.java:2307)
        at java.awt.Window.dispatchEventImpl(Window.java:2772)
        at java.awt.Component.dispatchEvent(Component.java:4843)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
        at java.awt.EventQueue$4.run(EventQueue.java:721)
        at java.awt.EventQueue$4.run(EventQueue.java:715)
        at java.security.AccessController.doPrivileged(AccessController.java:105)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
        at java.awt.EventQueue$5.run(EventQueue.java:745)
        at java.awt.EventQueue$5.run(EventQueue.java:743)
        at java.security.AccessController.doPrivileged(AccessController.java:105)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
        at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:567)
        at com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine(WindowsJavaThreads.java:138)

Actually, even more events seem to be non-implemented:

Exception in thread "AWT-Windows": java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: Ljava/awt/event/MouseWheelEvent;
        at com.oracle.svm.jni.functions.JNIFunctions.FindClass(JNIFunctions.java:346)
        at sun.awt.windows.WToolkit.eventLoop(WToolkit.java)
        at sun.awt.windows.WToolkit.run(WToolkit.java:305)
        at java.lang.Thread.run(Thread.java:829)
        at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:567)
        at com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine(WindowsJavaThreads.java:138)
Exception in thread "AWT-Windows": java.lang.NoSuchMethodError
java.lang.NoSuchMethodError: java.awt.event.KeyEvent.<init>(Ljava/awt/Component;IJIICI)V
        at com.oracle.svm.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1114)
        at com.oracle.svm.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1099)
        at com.oracle.svm.jni.functions.JNIFunctions.GetMethodID(JNIFunctions.java:410)
        at sun.awt.windows.WToolkit.eventLoop(WToolkit.java)
        at sun.awt.windows.WToolkit.run(WToolkit.java:305)
        at java.lang.Thread.run(Thread.java:829)
        at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:567)
        at com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine(WindowsJavaThreads.java:138)

Interacting with the JTextArea is rough and throws exceptions.

image

Neither of those exceptions come up with the regular executable Jar. Other than that, the toy application works now. 👍
If I'm required to trace all possible Swing GUI events before being about to properly compile, that's not very practical, even for a toy program.

@munishchouhan
Copy link
Contributor

@BullyWiiPlaza AWT and wing support for windows is still a work in progress, please follow the below issue for more information
#3084

@munishchouhan
Copy link
Contributor

Closing this because there is no activity in last 30 days

@JFWPublic
Copy link

This issue seems to still exist. I just compiled my simple Swing application and upon running I get:
Exception in thread "main" java.lang.NoSuchMethodError: java.awt.Toolkit.getDefaultToolkit()Ljava/awt/Toolkit;

I'm using GraalVM for Java 17 Windows amd64 v22.2.0

@ShanGor
Copy link

ShanGor commented Nov 24, 2022

You can manually copied some small files of the JAVA_HOME\lib to solve the problem in windows. And run with -Djava.home=.
Ref to https://jianshu.com/p/a53ae350f845?v=1669292961020

@tlf30
Copy link

tlf30 commented May 8, 2023

For anyone who gets here troubleshooting, here was my solution:
In gradle, I created a target that copies the compiled natives into build/dist and the necessary jdk files into build/dist/lib

task nativeDist(dependsOn: nativeCompile) {
    doLast {
        copy {
            mkdir 'build/dist'
            from 'build/native/nativeCompile'
            into 'build/dist'
            include '*.exe'
            include '*.dll'
        }
        println System.properties['java.home']
        copy {
            mkdir 'build/dist/lib'
            from System.properties['java.home'] + '/lib'
            into 'build/dist/lib'
            include 'fontconfig.bfc'
            include 'fontconfig.properties.src'
            include 'psfont.properties.ja'
            include 'psfontj2d.properties'
        }
    }
}

At the very beginning of my app (Very first thing in main) I did this:

 //GraalVM Fix
if (System.getProperty("java.home") == null) {
    System.out.println("No Java Home set, assuming that we are running from GraalVM. Fixing...");
    System.setProperty("java.home", new File(".").getAbsolutePath());
}

This causes the swing font data to get looked up from the lib/ dir next to the executable.

@risharde
Copy link

You can manually copied some small files of the JAVA_HOME\lib to solve the problem in windows. And run with -Djava.home=. Ref to https://jianshu.com/p/a53ae350f845?v=1669292961020

Am I misunderstanding the purpose of GraalVM Native Image? Shouldn't the executable not be dependent on the java.home files? I'm having the same issue as the original user. This is so confusing.

@YektaDev
Copy link

The Swing/AWT issue still exists with the latest version of JDK 20 (graalvm-community-openjdk-20.0.2+9.1)
Even with providing the configurations, I face the below exception upon execution (on Windows 11):

Exception in thread "main" java.lang.NoSuchMethodError: java.awt.Toolkit.getDefaultToolkit()Ljava/awt/Toolkit;
        at org.graalvm.nativeimage.builder/com.oracle.svm.core.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1267)
        at org.graalvm.nativeimage.builder/com.oracle.svm.core.jni.functions.JNIFunctions$Support.getMethodID(JNIFunctions.java:1252)
        at org.graalvm.nativeimage.builder/com.oracle.svm.core.jni.functions.JNIFunctions.GetStaticMethodID(JNIFunctions.java:420)

I know #3084 is still open, but it's been almost three years and I hope it gets addressed one day...

@lyjia
Copy link

lyjia commented Aug 25, 2023

Hi,

I just want to note that the java.lang.NoSuchMethodError: java.awt.Toolkit.getDefaultToolkit()Ljava/awt/Toolkit seems to be resolvable by running the java agent that outputs to target/native/agent-out, per https://www.graalvm.org/22.2/reference-manual/native-image/guides/use-native-image-maven-plugin/ (section "Build a Native Executable with Maven"), but when I do this the produced executable then throws java.lang.Error: java.home property not set.

Using graalvm 20 on Windows, compiling a Swing app with FlatLaf and miglayout (and no other dependencies other than what the tutorial linked here advises)

Not sure that setting JAVA_HOME env var is viable, I am trying to produce a standalone EXE for users without JDK/JRE installed

@tlf30
Copy link

tlf30 commented Aug 26, 2023

Please see my note above: #3659 (comment)

No need to have the jvm, just a couple files from it along side the exe.

Still, I am amazed that this has not been fixed by the graalvm team, it just shows how immature graalvm is still.

@lyjia
Copy link

lyjia commented Aug 26, 2023

Please see my note above: #3659 (comment)

No need to have the jvm, just a couple files from it along side the exe.

Still, I am amazed that this has not been fixed by the graalvm team, it just shows how immature graalvm is still.

Indeed. I had to translate your gradle fragment to maven and I think I may have gotten it working, but my native EXE is now throwing tons of java.lang.Error: no ComponentUI class for every Swing component I am using and the UI does not draw. I haven't had a chance to look into why yet.

In case it is useful to anyone else, the maven script is (put this in the <build> section of your 'native-image' profile):

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>3.2.0</version>
                        <executions>
                            <execution>
                                <id>copy-native</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/dist</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${project.build.directory}/native/nativeCompile</directory>
                                            <includes>
                                                <include>*.exe</include>
                                                <include>*.dll</include>
                                            </includes>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>3.2.0</version>
                        <executions>
                            <execution>
                                <id>copy-runtime-files</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/dist/lib</outputDirectory>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>${graalvm.groupId}</groupId>
                                            <artifactId>${graalvm.groupId}</artifactId>
                                            <version>${graalvm.version}</version>
                                            <overWrite>false</overWrite>
                                            <includes>
                                                fontconfig.bfc,fontconfig.properties.src,psfont.properties.ja,psfontj2d.properties
                                            </includes>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

@zlbProject
Copy link

请参阅我上面的注释:#3659(评论)
不需要 jvm,只需从其中的几个文件和 exe 一起即可。
尽管如此,令我惊讶的是 graalvm 团队还没有修复这个问题,这只是表明 graalvm 仍然不成熟。

的确。我必须将你的 gradle 片段转换为 Maven,我想我可能已经让它工作了,但是我的本机 EXE 现在java.lang.Error: no ComponentUI class为我正在使用的每个 Swing 组件抛出了大量的内容,并且 UI 不绘制。我还没有机会探究原因。

如果它对其他人有用,maven 脚本是(将其放在<build>“native-image”配置文件的部分中):

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>3.2.0</version>
                        <executions>
                            <execution>
                                <id>copy-native</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/dist</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${project.build.directory}/native/nativeCompile</directory>
                                            <includes>
                                                <include>*.exe</include>
                                                <include>*.dll</include>
                                            </includes>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>3.2.0</version>
                        <executions>
                            <execution>
                                <id>copy-runtime-files</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/dist/lib</outputDirectory>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>${graalvm.groupId}</groupId>
                                            <artifactId>${graalvm.groupId}</artifactId>
                                            <version>${graalvm.version}</version>
                                            <overWrite>false</overWrite>
                                            <includes>
                                                fontconfig.bfc,fontconfig.properties.src,psfont.properties.ja,psfontj2d.properties
                                            </includes>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

‘graalvm.groupId’ and ‘graalvm.version’ what is it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants