我在测试环境中运行一个进程,运行时间超过10小时,并使用Jasper Reports v3.7.5生成PDF文档。
该过程经常成功完成,但在某些情况下,该过程无法抛出此异常:
20/05/2017 02:45:23.503 ERROR [process-pool-2-thread-20] net.sf.jasperreports.extensions.DefaultExtensionsRegistry - Error instantiating extensions registry for simple.font.families
net.sf.jasperreports.engine.JRRuntimeException: java.io.IOException: Problem reading font data.
at net.sf.jasperreports.engine.fonts.SimpleFontFace.<init>(SimpleFontFace.java:77)
at net.sf.jasperreports.engine.fonts.SimpleFontFamily.createFontFace(SimpleFontFamily.java:316)
at net.sf.jasperreports.engine.fonts.SimpleFontFamily.setNormal(SimpleFontFamily.java:85)
at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontFamily(SimpleFontExtensionHelper.java:233)
at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontFamilies(SimpleFontExtensionHelper.java:204)
at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontFamilies(SimpleFontExtensionHelper.java:173)
at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontFamilies(SimpleFontExtensionHelper.java:142)
at net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory.createRegistry(SimpleFontExtensionsRegistryFactory.java:63)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.instantiateRegistry(DefaultExtensionsRegistry.java:238)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:213)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:162)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getRegistries(DefaultExtensionsRegistry.java:132)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:104)
at net.sf.jasperreports.engine.util.JRStyledTextParser.<clinit>(JRStyledTextParser.java:76)
at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:182)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:77)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:87)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:57)
at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:142)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:78)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:624)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:605)
...
Caused by: java.io.IOException: Problem reading font data.
at java.awt.Font.createFont0(Font.java:1000)
at java.awt.Font.createFont(Font.java:877)
at net.sf.jasperreports.engine.fonts.SimpleFontFace.<init>(SimpleFontFace.java:69)
... 120 common frames omitted
不久之后,在同一个线程上,会记录以下错误。不过我不确定它是否相关:
20/05/2017 02:45:23.605 ERROR [process-pool-2-thread-20] my.package.MyClass.NoClassDefFoundError - AbstractReportCreationService.createAndPersistReport(...) threw an error: Could not initialize class sun.awt.X11GraphicsEnvironment
我尝试使用相同的Java 1.8.0版本在Solaris 5.10和5.11上运行此过程。它在两者上随机发生。我一直试图重现错误以找到根本原因,但到目前为止无济于事。
我已经阅读了StackOverflow和Jaspersoft社区论坛中报告的类似问题。这些帖子中的大多数提到了可能的权限问题,导致进程无法读取或写入java临时目录(java.io.tmpdir = /var/tmp/
)。例如见:
The method JasperFillManager.fillReport () throws java.io.Exception
我已检查过文件权限,这些都是正确的。当进程成功运行时,它会创建名为+~JF9070759829719582131.tmp
或类似随机名称的临时字体文件。即使进程失败,它也会在/var/tmp/
目录中留下一些临时字体文件,因此权限似乎不是问题。最重要的是,在成功和失败的运行之间不会更改权限。
在这篇发布到Jaspersoft社区论坛的帖子中:
http://community.jaspersoft.com/questions/543492/javaioioexception-problem-reading-font-data
建议的解决方案是使用选项
启动Tomcat-Djava.awt.headless=true
我正在运行的进程使用普通的Java,而不是Tomcat,但我愿意尝试无头模式。由于问题是随机发生的,并且需要很长时间才能运行,因此很难证明这种可能的解决方案实际上解决了这个问题。我担心将其部署到生产环境并随机获得相同的问题。任何人都可以解释为什么在无头模式下运行可能会解决问题,或者我应该尝试其他可能的解决方案吗?
答案 0 :(得分:3)
我们在代码中解决了这个完全相同的问题。
它会随机发生。我们解决了实际问题被java吞并并在异常
下伪装Caused by: java.io.IOException: Problem reading font data.
at java.awt.Font.createFont0(Font.java:1000)
at java.awt.Font.createFont(Font.java:877)
at net.sf.jasperreports.engine.fonts.SimpleFontFace.<init>(SimpleFontFace.java:69)
... 120 common frames omitted
所以我编写了一段代码,它将复制创建字体所需的所有功能,但不会吞下真正的异常。我们在屏幕上运行了这个过程(我们可以附加和分离的服务器端会话)24小时没有任何问题。
直到我们让用户最初启动问题功能,才在屏幕上执行新的测试功能。产生了以下错误 -
java.io.IOException: Problem reading font data.
at judson.Main.createFont0(Main.java:203)
at judson.Main.createFont(Main.java:94)
at judson.Main.createFont(Main.java:49)
at judson.Main.main(Main.java:39)
Caused by: java.awt.AWTError: Can't connect to X11 window server using 'localhost:12.0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
事实证明,用户正在使用putty manager,它自动设置了DISPLAY变量。
/home/user/judsona/tmp/FontErrorDetector/bin : echo $DISPLAY
localhost:15.0
进一步调查为什么它偶尔失败导致我们发现当会话可用时(X11可用)它会起作用,但是一旦会话断开它就会失败。注意:即使初始用户从屏幕会话中分离,也会发生这种情况,它会等到他们在分离之前关闭会话之后才会失败
所以我调查了GraphicsEnvironment代码&amp; X11GraphicsEnvironment
X11GraphicsEnvironment.java
static {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
System.loadLibrary("awt");
/*
* Note: The MToolkit object depends on the static initializer
* of X11GraphicsEnvironment to initialize the connection to
* the X11 server.
*/
if (!isHeadless()) {
// first check the OGL system property
boolean glxRequested = false;
String prop = System.getProperty("sun.java2d.opengl");
if (prop != null) {
if (prop.equals("true") || prop.equals("t")) {
glxRequested = true;
} else if (prop.equals("True") || prop.equals("T")) {
glxRequested = true;
glxVerbose = true;
}
}
// Now check for XRender system property
boolean xRenderRequested = true;
boolean xRenderIgnoreLinuxVersion = false;
String xProp = System.getProperty("sun.java2d.xrender");
if (xProp != null) {
if (xProp.equals("false") || xProp.equals("f")) {
xRenderRequested = false;
} else if (xProp.equals("True") || xProp.equals("T")) {
xRenderRequested = true;
xRenderVerbose = true;
}
if(xProp.equalsIgnoreCase("t") || xProp.equalsIgnoreCase("true")) {
xRenderIgnoreLinuxVersion = true;
}
}
// initialize the X11 display connection
initDisplay(glxRequested);
^当它确定它不是无头时它将是initDisplay。
GraphicsEnvironment.java
/**
* @return the value of the property "java.awt.headless"
* @since 1.4
*/
private static boolean getHeadlessProperty() {
if (headless == null) {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Object>() {
public Object run() {
String nm = System.getProperty("java.awt.headless");
if (nm == null) {
/* No need to ask for DISPLAY when run in a browser */
if (System.getProperty("javaplugin.version") != null) {
headless = defaultHeadless = Boolean.FALSE;
} else {
String osName = System.getProperty("os.name");
if (osName.contains("OS X") && "sun.awt.HToolkit".equals(
System.getProperty("awt.toolkit")))
{
headless = defaultHeadless = Boolean.TRUE;
} else {
headless = defaultHeadless =
Boolean.valueOf(("Linux".equals(osName) ||
"SunOS".equals(osName) ||
"FreeBSD".equals(osName) ||
"NetBSD".equals(osName) ||
"OpenBSD".equals(osName)) &&
(System.getenv("DISPLAY") == null));
}
}
} else if (nm.equals("true")) {
headless = Boolean.TRUE;
} else {
headless = Boolean.FALSE;
}
return null;
}
}
);
}
return headless.booleanValue();
}
当设置DISPLAY变量时,java确定它不是无头的,这意味着它将尝试建立与X11服务器的连接(当用户关闭其会话时将断开连接,导致GraphicsEnvironment无法启动)
结论 -
你有三个选择
我个人会使用#1 -Djava.awt.headless = true