RFR: 8298266: "java.home property not set" error in Graal when sun.awt.fontconfig java property is set on Windows [v2]
Phil Race
prr at openjdk.org
Fri Dec 23 18:56:50 UTC 2022
On Thu, 22 Dec 2022 10:57:15 GMT, Alexander Scherbatiy <alexsch at openjdk.org> wrote:
>> **Environment**:
>> GraalVM 22.3.0 with jdk 19, Windows OS.
>>
>> **Description**:
>> Create a native image of Swing application using GraalVM and run it with ` -Dsun.awt.fontconfig=fontconfig.properties.src` java property.
>> `java.lang.Error: java.home property not set` error is thrown.
>>
>> For more details see https://github.com/oracle/graal/issues/4875.
>>
>> **Solution**:
>> The error is thrown by the code from [FontConfiguration.findFontConfigFile()](https://github.com/openjdk/jdk19u/blob/c9d485792b99233f381dcdfd69838e7b973909bd/src/java.desktop/share/classes/sun/awt/FontConfiguration.java#L175) method.
>> GraalVM does not set `java.home` property, that is why the `java.lang.Error: java.home property not set` is thrown.
>> `FontConfiguration.findFontConfigFile()` method compares `java.home` property to null before checking user provided `sun.awt.fontconfig` java property.
>>
>> The proposed fix swaps the order of `java.home` and `sun.awt.fontconfig` properties checking to avoid using `java.home` property in case `sun.awt.fontconfig` is set.
>>
>>
>> **Steps to reproduce**:
>> - Download graal 22.3.0 jdk 19 for Windows from [GraalVM Community Edition 22.3.0](https://github.com/graalvm/graalvm-ce-builds/releases/tag/vm-22.3.0) page.
>> - Install native-image
>>
>> graalvm-ce-java19-22.3.0\bin\gu.cmd install native-image
>>
>> - Create and compile `SwingSample.java` sample
>>
>> import java.awt.*;
>> import javax.swing.*;
>>
>> public class SwingSample {
>> public static void main(String[] args) throws Exception {
>> SwingUtilities.invokeAndWait(() -> {
>>
>> JFrame frame = new JFrame("Hello World");
>>
>> JButton button = new JButton("Hello");
>> button.addActionListener(e -> {
>> System.out.printf("Hello, World!%n");
>> });
>>
>> JPanel panel = new JPanel(new FlowLayout());
>> panel.add(button);
>>
>> frame.add(panel);
>> frame.setSize(400, 300);
>> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>> frame.setVisible(true);
>> });
>> }
>> }
>>
>> - Run native-image-agent to generate configuration files
>>
>> graalvm-ce-java19-22.3.0\bin\java.cmd -agentlib:native-image-agent=config-output-dir=conf-dir SwingSample
>>
>> - Copy `graalvm-ce-java19-22.3.0\lib\fontconfig.properties.src` file to the sample dir
>> - Generate native image with configuration files and `-Djava.awt.headless=false `, `-Dsun.awt.fontconfig=fontconfig.properties.src` java properties
>>
>> call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64"
>> graalvm-ce-java19-22.3.0\bin\native-image.cmd --no-fallback -Djava.awt.headless=false -Dsun.awt.fontconfig=fontconfig.properties.src -H:ResourceConfigurationFiles=conf-dir/resource-config.json -H:SerializationConfigurationFiles=conf-dir/serialization-config.json -H:ReflectionConfigurationFiles=conf-dir/reflect-config.json -H:JNIConfigurationFiles=conf-dir/jni-config.json SwingSample
>>
>> - Run the native image with `-Dsun.awt.fontconfig=fontconfig.properties.src` java property.
>>
>> swingsample.exe -Dsun.awt.fontconfig=fontconfig.properties.src
>>
>> Exception in thread "main" java.lang.reflect.InvocationTargetException
>> at java.desktop at 19.0.1/java.awt.EventQueue.invokeAndWait(EventQueue.java:1371)
>> at java.desktop at 19.0.1/java.awt.EventQueue.invokeAndWait(EventQueue.java:1346)
>> at java.desktop at 19.0.1/javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1480)
>> at SwingSample.main(SwingSample.java:7)
>> Caused by: java.lang.Error: java.home property not set
>> at java.desktop at 19.0.1/sun.awt.FontConfiguration.findFontConfigFile(FontConfiguration.java:180)
>> at java.desktop at 19.0.1/sun.awt.FontConfiguration.<init>(FontConfiguration.java:97)
>> at java.desktop at 19.0.1/sun.awt.windows.WFontConfiguration.<init>(WFontConfiguration.java:41)
>
> Alexander Scherbatiy has updated the pull request incrementally with one additional commit since the last revision:
>
> Initialize javaLib when a user fontconfig file is set
> There is the `Java system properties are inconsistent between traditional JDK and native image` issue in Graal: [oracle/graal#2835](https://github.com/oracle/graal/issues/2835)
>
> With comment: [oracle/graal#2835 (comment)](https://github.com/oracle/graal/issues/2835#issuecomment-1333300103)
>
> > We are still not setting the `java.home` system property at image run time by default, and do not plan to change that. Because there is just no "JDK home directory" around at image run time, so setting `java.home` to, for example, the directory where the native image is located in is misleading - files that might be expected to be in that directory are not there.
Surely that's graal's problem to solve.
This is a standard system property, it is an error if it is not set correctly.
Also sun.awt.config is not a supported documented property. It was put there for debugging.
If user code is relying on that (I'm not sure if that is being implied) then it is on shaky ground.
We could remove that at any time without notice.
-------------
PR: https://git.openjdk.org/jdk/pull/11559
More information about the client-libs-dev
mailing list