0

使用以下代码:

public static void main(String[] args) throws IOException {
    FileInputStream is = new FileInputStream("C:/Users/hp/Downloads/sampPPT.ppt");

    HSLFSlideShow ppt = new HSLFSlideShow(is);
    is.close();
    Dimension pgsize = ppt.getPageSize();
    int idx = 1;
    for (HSLFSlide slide : ppt.getSlides()) {
        BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();
        // clear the drawing area
        graphics.setPaint(Color.white);
        graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
        // render
        slide.draw(graphics);
        // save the output
        FileOutputStream out = new FileOutputStream("C:/Users/hp/Downloads/slide-" + idx + ".png");
        javax.imageio.ImageIO.write(img, "png", out);
        out.close();
        idx++;
    }

}

这会引发以下异常:

Exception in thread "main" java.lang.IllegalAccessError: class org.apache.poi.hslf.usermodel.HSLFSlideShowImpl tried to access private field org.apache.poi.POIDocument.directory (org.apache.poi.hslf.usermodel.HSLFSlideShowImpl and org.apache.poi.POIDocument are in unnamed module of loader 'app')
    at org.apache.poi.hslf.usermodel.HSLFSlideShowImpl.readCurrentUserStream(HSLFSlideShowImpl.java:340)
    at org.apache.poi.hslf.usermodel.HSLFSlideShowImpl.<init>(HSLFSlideShowImpl.java:154)
    at org.apache.poi.hslf.usermodel.HSLFSlideShowImpl.<init>(HSLFSlideShowImpl.java:127)
    at org.apache.poi.hslf.usermodel.HSLFSlideShowImpl.<init>(HSLFSlideShowImpl.java:116)
    at org.apache.poi.hslf.usermodel.HSLFSlideShow.<init>(HSLFSlideShow.java:138)
    at PPTConv.PPTConv.main(PPTConv.java:27)
4

1 回答 1

2

回答为什么会出现这种异常。也许它对其他人也有帮助:

Apache POI如果您混合来自不同版本的 jar,则会发生这种异常。这是不支持的。请参阅常见问题解答

在那种特殊情况下,类路径中可能有poi-*.jar不同poi-scratchpad-*.jar的版本。org.apache.poi.hslf.usermodel.HSLFSlideShowImpl扩展的类org.apache.poi.POIDocument包含在其中,poi-scratchpad-*.jarorg.apache.poi.POIDocument包含在 中 poi-*.jar。如果这些*.jars 来自不同的版本,则可能会发生以下情况:

代码行 340org.apache.poi.hslf.usermodel.HSLFSlideShowImpl中的调用poi-scratchpad-3.15.jar。这是可能的,因为它扩展并且在版本( )中有字段。currentUser = new CurrentUserAtom(directory);org.apache.poi.POIDocumentprotected DirectoryNode directory;3.15poi-3.15.jar

但是同一类org.apache.poi.POIDocument的 version 3.16( poi-3.16.jar) 有 field private DirectoryNode directory;。因此,如果代码行 340org.apache.poi.hslf.usermodel.HSLFSlideShowImpl中的版本3.15调用,但来自版本,则被抛出,因为它现在真的试图访问一个私有字段。currentUser = new CurrentUserAtom(directory);org.apache.poi.POIDocument3.16java.lang.IllegalAccessError: class org.apache.poi.hslf.usermodel.HSLFSlideShowImpl tried to access private field org.apache.poi.POIDocument.directory

于 2020-05-20T09:40:35.490 回答