0

I am passing a file as input stream to parser.parse() method while using apache tika library to convert file to text.The method throws an exception (displayed below) but the input stream is closed in the finally block successfully. Then while renaming the file, the File.renameTo method from java.io returns false. I am not able to rename/move the file despite successfully closing the inputStream. I am afraid another instance of file is created, while parser.parse() method processess the file, which doesn't get closed till the time exception is throw. Is that possible? If so what should I do to rename the file.

The Exception thrown while checking the content type is

java.lang.NoClassDefFoundError: Could not initialize class com.adobe.xmp.impl.XMPMetaParser
at com.adobe.xmp.XMPMetaFactory.parseFromBuffer(XMPMetaFactory.java:160)
at com.adobe.xmp.XMPMetaFactory.parseFromBuffer(XMPMetaFactory.java:144)
at com.drew.metadata.xmp.XmpReader.extract(XmpReader.java:106)
at com.drew.imaging.jpeg.JpegMetadataReader.extractMetadataFromJpegSegmentReader(JpegMetadataReader.java:112)
at com.drew.imaging.jpeg.JpegMetadataReader.readMetadata(JpegMetadataReader.java:71)    
at org.apache.tika.parser.image.ImageMetadataExtractor.parseJpeg(ImageMetadataExtractor.java:91) 
at org.apache.tika.parser.jpeg.JpegParser.parse(JpegParser.java:56)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:244)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:244)
at org.apache.tika.parser.AutoDetectParser.parse(AutoDetectParser.java:121)

Please suggest any solution. Thanks in advance.

 public static void main(String args[])
 {
  InputStream is = null; 
  StringWriter writer = new StringWriter();
  Metadata metadata = new Metadata();
  Parser parser = new AutoDetectParser();
  File file = null;
  File destination  = null;
  try
  {
   file = new File("E:\\New folder\\testFile.pdf");
   boolean a = file.exists();
   destination = new File("E:\\New folder\\test\\testOutput.pdf");
   is = new FileInputStream(file);
   parser.parse(is, new WriteOutContentHandler(writer), metadata, new ParseContext()); //EXCEPTION IS THROWN HERE.
   String contentType = metadata.get(Metadata.CONTENT_TYPE);
   System.out.println(contentType);
   
  }
  catch(Exception e1)
  {
   e1.printStackTrace();
  }
  catch(Throwable t)
  {
   t.printStackTrace();
  }
  finally
  {
   try
   {
    if(is!=null)
    {
     is.close(); //CLOSES THE INPUT STREAM
    }
    writer.close();
   }
   catch(Exception e2)
   {
    e2.printStackTrace();
   }
   
  }
  boolean x = file.renameTo(destination); //RETURNS FALSE
  System.out.println(x);
 }
  • 1
    Looks like you are missing a dependency - the stacktrace shows the error during Tika parsing, not during the attempt to move. Make sure you have the jar with that class on your classpath. – cjstehno Oct 20 '14 at 12:54
  • If you can post source code, it will help others to analyse the problem.. – CuriousMind Oct 20 '14 at 13:05
  • I have added the code. please suggest any solution. – Deepanshu Bhardwaj Oct 21 '14 at 10:41
  • file.delete() method is returning true for the same file while file.renameTo() is returning false. what might be the reason. But in some cases files are being renamed successfully. Is it a problem of file system? – Deepanshu Bhardwaj Oct 27 '14 at 12:03

1 Answers1

0

This might be due to other processes are still using the file, like anti-virus program and also it may be a case that any other processes in your application may possessing a lock. please check that and deal with that, it may solve your problem.

asvni
  • 373
  • 3
  • 10