2010-02-10 4 views
0

이것은 상황입니다. 이미 PNG 세대가있는 프로그램에 PDF 생성을 추가해야합니다. 초기에 관련된 두 클래스는 다음과 같습니다. ActionUsuels 여기서부터 CaptureImage3D의 생성자가 호출됩니다.JAVA - 이상한 NoClassDefFoundError : com/lowagie/textDocumentException

PDF 생성을 추가 할 때 CaptureImage3D 클래스에 메소드를 추가했습니다. PDF 생성을 추가하기 전에 PNG 생성이 올바르게 수행되었습니다. 하지만 이제 PNG 생성을 시도하면 NoClassDefFoundError : com/lowagie/text/DocumentException이 표시됩니다. DocumentException합니다 (iText를 항아리에서) 클래스 패스에서 읽을 수 있지만 할 수 없습니다 :

나는이 클래스는 것을 의미한다 알고

  1. PDF로 생성 메소드가 호출되지 않습니다.
  2. CaptureImage3D의 생성자를 입력하기 전에 예외가 생성됩니다. 나는 모든 시도/캐치 진영을 언급 할 때

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(); 
     } 


    } 

이 모든 것이 잘 작동! :

  • 는 다음 PDF 생성 방법을 고려

    다시 반복합니다. captureImagePDF가 호출되지 않습니다. 심지어 CaptureImage3D의 생성자도 액세스하지 않습니다. (해야하지만 예외는 전에 제기 된). 그리고 네, 저는 classpath에 itext 있습니다.

    어디서나 호출되지 않는 코드 조각이 예외의 유령을 유발한다는 사실을 알게되었습니다.

    주저하지 말고 설명을 요청하십시오!

    아이디어가 있으십니까?

    당신에게

  • +1

    누락 된 클래스를 클래스 경로에 넣지 않는 이유는 무엇입니까? – ron

    답변

    3

    당신이 DocumentException의 캐치 로더 시스템이 그것을 잡을 수 있도록 클래스를로드한다는 것을 의미 있다는 사실을 감사드립니다. :-)

    클래스 패스에 iText jar 파일을 가지고있을 필요가 없도록하려면, 더 높은 것을 잡으십시오. 또는 말한 것처럼 잡아 내지 마십시오. :-P

    +1

    이것은 맞습니다. 클래스 로더는 실행 코드에 "정적으로"필요한 모든 클래스를로드합니다. – Romain

    +0

    하지만 특정 클래스를 가져 왔습니다. import com.lowagie.text.DocumentException; Java 빌드 경로 -> 라이브러리에 itext jar를 넣었습니다. 무엇이 잘못 되었습니까? –

    +0

    @Amokrane : 빌드 경로는 빌드 용 jar를 나열합니다. 클래스 패스에 동일한 병이 있어야 런타임에 사용할 수 있습니다. :-) –

    관련 문제