javax.ImageIO 方法静默失败 [英] javax.ImageIO methods fail silently
问题描述
如果我的 java 代码调用任何 javax.ImageIO 方法,它会抛出一个无提示错误.例如
If my java code makes a call to any javax.ImageIO method, it throws a silent error. e.g.
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try
{
BufferedImage fullImg = ImageIO.read(screenshot);
}
catch(Exception e)
{
e.printStackTrace();
}
没有产生错误信息,但代码在那个点停止.我尝试调用 ImageIO.getReaderFormatNames() 只是为了查看,但它也会抛出一个无提示错误.在安装了 oracle jre(java 版本1.8.0_60")的 Ubuntu 中会出现此问题.请注意,相同的代码在 Windows (10) 中运行良好.我也试过 FileInputStream
no error message is produced, but the code halts at that point. I tried calling ImageIO.getReaderFormatNames() just to see, but it also throws a silent error. This problem occurs in Ubuntu with oracle jre (java version "1.8.0_60") installed. Please note that the same code works perfectly fine in Windows (10). I have tried with FileInputStream as well
FileInputStream fis = new FileInputStream(screenshot);
BufferedImage fullImg = ImageIO.read(fis);
推荐答案
在@MadProgrammer 建议我捕获 Throwable 而不是 Exception 进行调试后,我解决了这个问题.我发现我的 Ubuntu 15.04 机器没有安装 libxtst6,导致以下错误
I solved the problem after @MadProgrammer suggested me to catch Throwable instead of Exception to debug. I found out that my Ubuntu 15.04 machine doesn't have libxtst6 installed, leading to the following error
java.lang.UnsatisfiedLinkError: /usr/lib/jvm/java-8-oracle/jre/lib/amd64/libawt_xawt.so: libXtst.so.6: cannot open shared object file: No such file or directory
最终导致以下 NoClassDefFoundError
which ended up causing the following NoClassDefFoundError
java.lang.NoClassDefFoundError: Could not initialize class javax.imageio.ImageIO
我的java版本是1.8.0_60希望它可以帮助其他面临类似问题的人.
My java version is 1.8.0_60 Hope it helps others facing similar issues.
这篇关于javax.ImageIO 方法静默失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!