8

I'm with my hands in my hair on this one.

I'm using Apache PDFBox, because I want to read pdf files line by line in JAVA and deal with the contents later on. However I have the following problem.. I've used the code below in a seperate java program (in the main method) and it works fine there. However when I use it in my tomcat server applet in combination with a quartz scheduler something goes wrong and I can't figure out why. Bear in mind I copy+pasted the lines below from the working seperate testing program into my own bigger project so it's the exact same code. However in my bigger project tHe program runs until the String x1 = .. line where I've put a breakpoint. When i try to step over it doesnt' give any errors, console output or anything and jumps over the catch clause straight to the finally. I've put breakpoints in the catch clause and they'er not being triggered.

this is my code:

    PDFTextStripper stripper;
    PDDocument doc = null;      
    try{
      doc = PDDocument.load("00026614_F_21Jan2013-18Feb2013.pdf");
      stripper = new PDFTextStripper();
      String x1= stripper.getText(doc);    //SOMETHING GOES WRONG HERE
      //break up the file content returned as a string into individual lines
      List<String> ans= Arrays.asList(x1.split("\r\n"));//THIS IS NEVER REACHED
    }
    catch(Exception e){
        e.printStackTrace();    //THIS IS NEVER REACHED EITHER
    }
    finally{
       if(doc!=null) //IT GOES FROM STRING X1 STRAIGHT TO HERE.
       doc.close();
    }

The only output i get is from the LOG4J which had to be initialized for some other part of the program and the final few lines of output are

34212 [DefaultQuartzScheduler_Worker-1] DEBUG org.apache.pdfbox.util.PDFStreamEngine  - processing substream token: PDFOperator{Tm}
34212 [DefaultQuartzScheduler_Worker-1] DEBUG org.apache.pdfbox.util.PDFStreamEngine  - processing substream token: COSName{ttf0}
34212 [DefaultQuartzScheduler_Worker-1] DEBUG org.apache.pdfbox.util.PDFStreamEngine  - processing substream token: COSInt{10}
34212 [DefaultQuartzScheduler_Worker-1] DEBUG org.apache.pdfbox.util.PDFStreamEngine  - processing substream token: PDFOperator{Tf}

Now the thing that bugs me is how can I figure out what goes wrong in that call? Like I said if I put the code in a seperate java program it runs fine.. but I"m not able to get any error output or catch any exceptions (I'm trying to catch Exception even though it should give IOException and yes I've tried that as well).

I hope any of you know what to do with this. Kind regards

Maurits
  • 93
  • 1
  • 4
  • I can guarantee you that java is not broken and it's not "jumping straight to finally". If the catch clause isn't invoked, that exception wasn't thrown. – Brian Roach Feb 23 '13 at 03:39
  • 1
    For debugging only, you could try catching `Throwable` instead of `Exception`, to see if you get anything. Thought here is an `Error` is not an `Exception` but is still throwable. – Charlie Feb 23 '13 at 03:41
  • 7
    It is most probably an `Error` was thrown not an `Exception`. You can try changing the `catch` block to `catch(Error e){` from `catch(Exception e){`. It is most probably a `NoClassDefFoundError` or a `NoSuchMethodError` error – Arun P Johny Feb 23 '13 at 03:41
  • I'm unable to change the status of this question to "solved" but it is in fact solved now! Thanks guys you were right on the spot! It's been a few years since I programmed in JAVA and so was under the impression that Exception was the highest thing you could catch. I changed the Exception to Error which indeed gave a NoClassDefFoundError. The reason why it worked on the standalone java app and not in my tomcat environment is because PDFBox has a dependency on a "apache fontbox jar", which I had in my JAVA build path but not in my tomcat LIB. This solved the problem. – Maurits Feb 23 '13 at 11:20

1 Answers1

7

Looks like someone already answered this, but adding a catch for the error did the trick for a similar problem I was having.

I added this 'catch' (for error) after the 'catch' for exception:

catch (Error err){
    err.printStackTrace();
    throw (err);
}
Nash N
  • 342
  • 2
  • 17