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

时间:2010-02-10 21:22:27

标签: java itext noclassdeffounderror

就是这种情况。我需要将PDF生成添加到已经生成PNG的程序中。最初涉及的两个类是: ActionUsuels 调用 CaptureImage3D 的构造函数。

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

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

  1. 永远不会调用PDF生成方法。
  2. 在输入CaptureImage3D的构造函数之前生成异常。
  3. 请考虑以下PDF生成方法:
  4. 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();
            }
    
    
      }
    

    当我评论所有的try / catch集团时,一切正常!

    我再说一次:永远不会调用captureImagePDF。甚至永远不会访问CaptureImage3D的构造函数。 (它应该是,但之前提出异常)。是的,我在类路径中有itext。

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

    请不要犹豫要求澄清!

    有什么想法吗?

    谢谢

1 个答案:

答案 0 :(得分:3)

你有DocumentException的捕获这一事实意味着加载器必须加载类,以便系统可以捕获它。 : - )

如果你想避免在你的类路径中使用iText jar,可以抓住更高的东西,或者(就像你说的那样)根本不要抓住它。 :-P