2010-02-10 23 views
0

這就是情況。我需要將PDF生成添加到已有PNG生成的程序中。最初涉及的2個類別是: ActionUsuels從那裏構造函數CaptureImage3D被調用。JAVA - 奇怪的NoClassDefFoundError in:com/lowagie/textDocumentException

當我添加PDF生成時,我在CaptureImage3D類中添加了一個方法。 在添加PDF生成之前,PNG生成工作正常。但是現在,當我嘗試進行PNG生成時,我得到:NoClassDefFoundErrorcom/lowagie/text/DocumentException

我知道這意味着該類:DocumentException(從和iText JAR)不能從classpath讀取,但:

  1. 生成PDF的方法不會被調用。
  2. 在進入構造函數CaptureImage3D之前生成異常。
  3. 考慮下面的PDF生成方法:

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

爲什麼不把缺少的類放在類路徑上? – ron 2010-02-10 21:26:17

回答

3

您擁有DocumentException的catch指裝載有加載類,從而使系統能夠趕上它的事實。 :-)

如果你想避免在你的類路徑中必須有iText jar,趕上更高的東西,或者(如你所說)不要抓住。 :-P

+1

這是正確的。類加載器將加載執行代碼所需的「靜態」所有類。 – Romain 2010-02-10 21:30:41

+0

但是我導入了具體的類: import com.lowagie.text.DocumentException; 我把itext jar放在Java Build Path - > Libraries 有什麼問題? – 2010-02-10 21:38:22

+0

@Amokrane:構建路徑列出了建築用的罐子。您還需要在類路徑中使用相同的jar,以便在運行時可用。 :-) – 2010-02-10 21:56:44