這就是情況。我需要將PDF生成添加到已有PNG生成的程序中。最初涉及的2個類別是: ActionUsuels
從那裏構造函數CaptureImage3D
被調用。JAVA - 奇怪的NoClassDefFoundError in:com/lowagie/textDocumentException
當我添加PDF生成時,我在CaptureImage3D
類中添加了一個方法。 在添加PDF生成之前,PNG生成工作正常。但是現在,當我嘗試進行PNG生成時,我得到:NoClassDefFoundError
:com/lowagie/text/DocumentException
。
我知道這意味着該類:DocumentException
(從和iText JAR)不能從classpath讀取,但:
- 生成PDF的方法不會被調用。
- 在進入構造函數
CaptureImage3D
之前生成異常。 - 考慮下面的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。
我覺得很奇怪的是,一段代碼在任何地方都不會被調用,導致異常的幻影!
不要猶豫,要求澄清!
有什麼想法?
謝謝
爲什麼不把缺少的類放在類路徑上? – ron 2010-02-10 21:26:17