<AWT Dev> 回复: 回复: An issue of OpenJRE

Alexey Ivanov alexey.ivanov at oracle.com
Wed Sep 7 12:11:09 UTC 2016


Hi Rong,

Using Java 2D, even in headless mode, implies there are font files in 
the system. If there are no fonts, it could fail.

So just install some fonts in your OS.


Regards,
Alexey

On 23.08.2016 13:13, 31731705 wrote:
> Hi Alexey,
>
> Sorry for so late response. To following codes
>
>         BufferedImage image = new BufferedImage(200, 200, 
> BufferedImage.TYPE_INT_RGB);
>         Graphics2D g2 = image.createGraphics();
>         FontMetrics fm = g2.getFontMetrics();
>
> If there are no any font files in system the code will cause 
> exception. The stack trace is below.
>
> Exception in thread "main" java.lang.NullPointerException
>         at 
> sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
>         at 
> sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
>         at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
>         at 
> sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:753)
>         at sun.font.SunFontManager$2.run(SunFontManager.java:430)
>         at java.security.AccessController.doPrivileged(Native Method)
>         at sun.font.SunFontManager.<init>(SunFontManager.java:375)
>         at sun.awt.X11FontManager.<init>(X11FontManager.java:32)
>         at 
> sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>         at 
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>         at 
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>         at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
>         at java.lang.Class.newInstance(Class.java:433)
>         at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
>         at java.security.AccessController.doPrivileged(Native Method)
>         at 
> sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
>         at sun.font.SunFontManager.getInstance(SunFontManager.java:249)
>         at 
> sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:264)
>         at sun.java2d.SunGraphics2D.getFontMetrics(SunGraphics2D.java:845)
>         at rptgraph.ReportGenerator.main(ReportGenerator.java:374)
>
> If I put the font file LucidaSansRegular.ttf into jre/lib/fonts folder 
> the code won't cause exception but fm will get wrong value whether I 
> call the the statement g2.setFont(new Font(Font.DIALOG, Font.PLAIN, 
> 12)) or not.
>
> Yes, you are right. "By placing font files into jre/lib/fonts you 
> change the behavior of the JRE, and you get unexpected results."
> And I just wonder why a simple font file will change behavior of 
> OpenJRE, sounds a little incredible.
>
> Regards,
> Rong
>
> ------------------ 原始邮件 ------------------
> *发件人:* "Alexey Ivanov";<alexey.ivanov at oracle.com>;
> *发送时间:* 2016年5月30日(星期一) 晚上6:12
> *收件人:* ""<31731705 at qq.com>; "Mario 
> Torre"<neugens.limasoftware at gmail.com>; "dalibor 
> topic"<dalibor.topic at oracle.com>;
> *抄送:* "awt-dev"<awt-dev at openjdk.java.net>;
> *主题:* Re: <AWT Dev> 回复: An issue of OpenJRE
>
> Hi Rong,
>
> OpenJDK comes without font files in its jre/lib. Does OpenJDK not work 
> as expected?
>
> By placing font files into jre/lib/fonts you change the behavior of 
> the JRE, and you get unexpected results. Just don't copy font files 
> into JRE directory. If you want to use additional fonts, install those 
> font according to your OS instructions.
>
> Oracle JDK has several font files in its jre/lib/fonts. Those fonts 
> are part of the JRE. If you remove them, you can get unexpected results.
>
>
> You say that the following code throws NullPointerException on the 
> third line:
>
>         BufferedImage image = new BufferedImage(200, 200, 
> BufferedImage.TYPE_INT_RGB);
>         Graphics2D g2 = image.createGraphics();
>         FontMetrics fm = g2.getFontMetrics();
>
> What is the current font selected into g2?
> Have you tried explicitly setting a font?
>
>         g2.setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
>
> The stacktrace of NullPointerException could also help to diagnose the 
> problem. If you think it's a problem, you can submit a bug at 
> http://bugreport.java.com/
>
> In the bug report, describe your environment (OS etc), your version of 
> Java, provide any error messages you get and a test case to reproduce 
> the problem.
>
>
> Regards,
> Alexey
>
> On 21.05.2016 7:23, 31731705 wrote:
>> <SNIP>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/awt-dev/attachments/20160907/c45d4790/attachment.html>


More information about the awt-dev mailing list