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.iva...@oracle.com>;
*????????:* 2016??5??30??(??????) ????6:12
*??????:* ""<31731...@qq.com>; "Mario
Torre"<neugens.limasoftw...@gmail.com>; "dalibor
topic"<dalibor.to...@oracle.com>;
*????:* "awt-dev"<awt-dev@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>