JAVA - 奇怪的 NoClassDefFoundError 位于:com/lowagie/textDocumentException

发布于 2024-08-20 19:48:20 字数 2548 浏览 5 评论 0原文

情况就是这样。我需要将 PDF 生成添加到已经具有 PNG 生成功能的程序中。最初涉及的 2 个类是: ActionUsuels 从这里调用 CaptureImage3D 的构造函数。

当我添加 PDF 生成时,我在 CaptureImage3D 类中添加了一个方法。 在添加 PDF 生成之前,PNG 生成工作正常。但现在,当我尝试生成 PNG 时,我得到一个: NoClassDefFoundError: com/lowagie/text/DocumentException

我知道这意味着类:DocumentException(来自itext jar)无法从类路径中读取,但是:

  1. 永远不会调用PDF生成方法。
  2. 该异常是在进入CaptureImage3D的构造函数之前生成的。
  3. 考虑以下 PDF 生成方法:

代码:

  public void captureImagePDF(File imageFile)
  {

        System.out.println("Pdf appelé");

        // Dimension (en pixels) de l'image a sauvegarder dans le fichier
        Dimension dim = new Dimension(512, 512);

        // On recupere l'image (pixmap) rendue par le canvas 3D offscreen
        BufferedImage myBufferedImage = offScreenCanvas.getOffScreenImage(dim);

        // On recupere le contexte graphique de l'image finale de sortie
        Graphics2D gc = myBufferedImage.createGraphics();

        gc.drawImage(myBufferedImage, 0, 0, null);

        Document myPDF = new Document(PageSize.A4, 50, 50, 50, 50);

        PdfWriter myWriter = null;

        try 
        {
            myWriter = PdfWriter.getInstance(myPDF, new FileOutputStream(imageFile));
        } 


        catch (FileNotFoundException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        catch (DocumentException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        myPDF.open();
        PdfContentByte cb = myWriter.getDirectContent();
        cb.saveState();
        Image image = null;

        try {
            image = Image.getInstance(myBufferedImage,null);
        } 

        catch (BadElementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
                cb.addImage(image);
        } 
        catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }


  }

当我注释所有 try/catch 块时,一切正常!

我再说一遍:captureImagePDF 从未被调用。甚至CaptureImage3D的构造函数也从未被访问过。 (应该是,但是之前已经引发了异常)。是的,我的类路径中有 itext。

我觉得很奇怪的是,一段从未在任何地方调用的代码会导致异常的出现!

请随时要求澄清!

有什么想法吗?

谢谢

This is the situation. I need to add PDF generation to a program that already has PNG generation. Initially the 2 classes involved are :
ActionUsuels From where the constructor of CaptureImage3D is called.

When I added the PDF generation I added a method at the CaptureImage3D class.
Before adding the PDF generation, the PNG generation worked correctly. But now when I try to do the PNG generation, I get a : NoClassDefFoundError: com/lowagie/text/DocumentException.

I know it means that the class : DocumentException (from the itext jar) can't be read from the classpath but :

  1. The PDF generation method is NEVER called.
  2. The exception is generated before entering the constructor of CaptureImage3D.
  3. Consider the following PDF generation method:

Code:

  public void captureImagePDF(File imageFile)
  {

        System.out.println("Pdf appelé");

        // Dimension (en pixels) de l'image a sauvegarder dans le fichier
        Dimension dim = new Dimension(512, 512);

        // On recupere l'image (pixmap) rendue par le canvas 3D offscreen
        BufferedImage myBufferedImage = offScreenCanvas.getOffScreenImage(dim);

        // On recupere le contexte graphique de l'image finale de sortie
        Graphics2D gc = myBufferedImage.createGraphics();

        gc.drawImage(myBufferedImage, 0, 0, null);

        Document myPDF = new Document(PageSize.A4, 50, 50, 50, 50);

        PdfWriter myWriter = null;

        try 
        {
            myWriter = PdfWriter.getInstance(myPDF, new FileOutputStream(imageFile));
        } 


        catch (FileNotFoundException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        catch (DocumentException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        myPDF.open();
        PdfContentByte cb = myWriter.getDirectContent();
        cb.saveState();
        Image image = null;

        try {
            image = Image.getInstance(myBufferedImage,null);
        } 

        catch (BadElementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
                cb.addImage(image);
        } 
        catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }


  }

When I comment all the try/catch blocs, everything works fine !!!

I repeat again: captureImagePDF is never called. And even the constructor of CaptureImage3D is never accessed. (it should be, but the exception is raised before). And yes, I have itext in the classpath.

I find it weird the fact that a piece of code, that is never called anywhere, causes the apparition of the exception!

Don't hesitate to ask for clarifications!

Any idea?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撧情箌佬 2024-08-27 19:48:20

事实上,您捕获了 DocumentException 意味着加载器必须加载该类,以便系统可以捕获它。 :-)

如果您想避免在类路径中包含 iText jar,请捕获更高层的内容,或者(就像您所说的)根本不捕获。 :-P

The fact that you have a catch for DocumentException means that the loader has to load the class, so that the system can catch it. :-)

If you want to avoid having to have the iText jar in your classpath, catch something higher up, or (like you said) don't catch at all. :-P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文