graalvm / native-build-tools

Native-image plugins for various build tools
https://graalvm.github.io/native-build-tools/
Other
349 stars 51 forks source link

Awt graphics2D.getFontMetrics() fails with java.home property not set #534

Closed ditogam closed 8 months ago

ditogam commented 8 months ago

Hello, can you help with this issue?

import java.awt.*;
import java.awt.image.BufferedImage;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
        Font font = new Font("Arial", Font.BOLD, 15);
        Graphics2D graphics2D = image.createGraphics();
        graphics2D.setFont(font);

        FontMetrics fontMetrics = graphics2D.getFontMetrics();
        System.out.println(fontMetrics.getHeight());
    }
}

javac Main java -agentpath:path_to_graalvm/lib/libnative-image-agent.so=config-output-dir=test Main

native-image -H:ConfigurationFileDirectories=test Main

Output


========================================================================================================================
GraalVM Native Image: Generating 'main' (executable)...
========================================================================================================================
[1/8] Initializing...                                                                                    (2.8s @ 0.19GB)
 Java version: 17.0.9+11-LTS, vendor version: Oracle GraalVM 17.0.9+11.1
 Graal compiler: optimization level: 2, target machine: x86-64-v3, PGO: ML-inferred
 C compiler: gcc (linux, x86_64, 11.4.0)
 Garbage collector: Serial GC (max heap size: 80% of RAM)
[2/8] Performing analysis...  [***]                                                                      (3.1s @ 0.96GB)
   4,122 (72.94%) of  5,651 types reachable
   5,939 (55.00%) of 10,799 fields reachable
  20,696 (48.19%) of 42,947 methods reachable
   1,355 types,     4 fields, and   605 methods registered for reflection
     122 types,   188 fields, and   108 methods registered for JNI access
       4 native libraries: dl, pthread, rt, z
[3/8] Building universe...                                                                               (0.6s @ 0.90GB)
[4/8] Parsing methods...      [*]                                                                        (1.2s @ 1.61GB)
[5/8] Inlining methods...     [***]                                                                      (0.2s @ 0.84GB)
[6/8] Compiling methods...    [***]                                                                      (6.7s @ 0.82GB)
[7/8] Layouting methods...    [*
]                                                                        (0.9s @ 0.83GB)
[8/8] Creating image...       [*]                                                                        (0.9s @ 0.68GB)
  10.45MB (53.42%) for code area:    10,911 compilation units
   8.30MB (42.45%) for image heap:  135,881 objects and 5 resources
 826.38kB ( 4.13%) for other data
  19.56MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area:                                Top 10 object types in image heap:
   6.36MB java.base                                            2.06MB byte[] for code metadata
   1.82MB svm.jar (Native Image)                               1.05MB byte[] for java.lang.String
   1.77MB java.desktop                                      1009.85kB java.lang.String
 170.17kB java.logging                                       950.52kB byte[] for general heap data
 142.68kB com.oracle.svm.svm_enterprise                      734.47kB java.lang.Class
  32.12kB org.graalvm.sdk                                    311.44kB java.util.HashMap$Node
  30.92kB org.graalvm.nativeimage.base                       193.22kB com.oracle.svm.core.hub.DynamicHubCompanion
  27.33kB jdk.crypto.ec                                      193.16kB byte[] for reflection metadata
  15.27kB jdk.internal.vm.compiler                           148.84kB byte[] for embedded resources
  13.30kB jdk.internal.vm.ci                                 141.99kB char[]
   6.00kB for 5 more packages                                  1.28MB for 959 more object types
------------------------------------------------------------------------------------------------------------------------
Recommendations:
 G1GC: Use the G1 GC ('--gc=G1') for improved latency and throughput.
 PGO:  Use Profile-Guided Optimizations ('--pgo') for improved throughput.
 HEAP: Set max heap for improved and more predictable memory usage.
 CPU:  Enable more CPU features with '-march=native' for improved performance.
 QBM:  Use the quick build mode ('-Ob') to speed up builds during development.
------------------------------------------------------------------------------------------------------------------------
                        0.8s (4.9% of total time) in 79 GCs | Peak RSS: 2.53GB | CPU load: 18.44
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
 /home/dito/programs/other/AwtTest/src/main/java/libawt.so (jdk_library)
 /home/dito/programs/other/AwtTest/src/main/java/libawt_headless.so (jdk_library)
 /home/dito/programs/other/AwtTest/src/main/java/libawt_xawt.so (jdk_library)
 /home/dito/programs/other/AwtTest/src/main/java/libfontmanager.so (jdk_library)
 /home/dito/programs/other/AwtTest/src/main/java/libjava.so (jdk_library_shim)
 /home/dito/programs/other/AwtTest/src/main/java/libjvm.so (jdk_library_shim)
 /home/dito/programs/other/AwtTest/src/main/java/liblcms.so (jdk_library)
 /home/dito/programs/other/AwtTest/src/main/java/main (executable)

Exception

Exception in thread "main" java.lang.InternalError: java.lang.reflect.InvocationTargetException
        at java.desktop@17.0.9/sun.font.FontManagerFactory$1.run(FontManagerFactory.java:87)
        at java.base@17.0.9/java.security.AccessController.executePrivileged(AccessController.java:171)
        at java.base@17.0.9/java.security.AccessController.doPrivileged(AccessController.java:318)
        at java.desktop@17.0.9/sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:75)
        at java.desktop@17.0.9/sun.font.SunFontManager.getInstance(SunFontManager.java:248)
        at java.desktop@17.0.9/sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:266)
        at java.desktop@17.0.9/sun.java2d.SunGraphics2D.getFontMetrics(SunGraphics2D.java:863)
        at Main.main(Main.java:11)
Caused by: java.lang.reflect.InvocationTargetException
        at java.base@17.0.9/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at java.base@17.0.9/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
        at java.desktop@17.0.9/sun.font.FontManagerFactory$1.run(FontManagerFactory.java:85)
        ... 7 more
Caused by: java.lang.Error: java.home property not set
        at java.desktop@17.0.9/sun.awt.FontConfiguration.findFontConfigFile(FontConfiguration.java:181)
        at java.desktop@17.0.9/sun.awt.FontConfiguration.<init>(FontConfiguration.java:98)
        at java.desktop@17.0.9/sun.font.MFontConfiguration.<init>(MFontConfiguration.java:48)
        at java.desktop@17.0.9/sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:698)
        at java.desktop@17.0.9/sun.font.SunFontManager$2.run(SunFontManager.java:358)
        at java.desktop@17.0.9/sun.font.SunFontManager$2.run(SunFontManager.java:315)
        at java.base@17.0.9/java.security.AccessController.executePrivileged(AccessController.java:171)
        at java.base@17.0.9/java.security.AccessController.doPrivileged(AccessController.java:318)
        at java.desktop@17.0.9/sun.font.SunFontManager.<init>(SunFontManager.java:315)
        at java.desktop@17.0.9/sun.awt.FcFontManager.<init>(FcFontManager.java:35)
        at java.desktop@17.0.9/sun.awt.X11FontManager.<init>(X11FontManager.java:56)
        ... 10 more

when specify java.home ./main -Djava.home=/home/dito/Music/graalvm-jdk-17.0.9+11.1


Caused by: java.lang.NoClassDefFoundError: sun/font/FontConfigManager$FontConfigInfo
        at org.graalvm.nativeimage.builder/com.oracle.svm.core.jni.functions.JNIFunctions.FindClass(JNIFunctions.java:350)
        at java.desktop@17.0.9/sun.font.FontConfigManager.getFontConfig(Native Method)
        at java.desktop@17.0.9/sun.font.FontConfigManager.initFontConfigFonts(FontConfigManager.java:191)
        at java.desktop@17.0.9/sun.font.FontConfigManager.loadFontConfig(FontConfigManager.java:448)
        at java.desktop@17.0.9/sun.font.FcFontConfiguration.init(FcFontConfiguration.java:96)
        at java.desktop@17.0.9/sun.font.FcFontConfiguration.<init>(FcFontConfiguration.java:74)
        at java.desktop@17.0.9/sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:700)
        at java.desktop@17.0.9/sun.font.SunFontManager$2.run(SunFontManager.java:358)
        at java.desktop@17.0.9/sun.font.SunFontManager$2.run(SunFontManager.java:315)
        at java.base@17.0.9/java.security.AccessController.executePrivileged(AccessController.java:171)
        at java.base@17.0.9/java.security.AccessController.doPrivileged(AccessController.java:318)
        at java.desktop@17.0.9/sun.font.SunFontManager.<init>(SunFontManager.java:315)
        at java.desktop@17.0.9/sun.awt.FcFontManager.<init>(FcFontManager.java:35)
        at java.desktop@17.0.9/sun.awt.X11FontManager.<init>(X11FontManager.java:56)
        ... 10 more
ditogam commented 8 months ago

I was executing with Oracle build of GraalVM, but it's the same issue with Java version: 17.0.9+9, vendor version: GraalVM CE 17.0.9+9.1 ()

dnestoro commented 8 months ago

Hey @ditogam, since this issue is not related to native build tools (you are not using Gradle or Maven), you should open a ticket like this on GraalVM Repository