4

Here is my method:

public String retrieveMimeType(InputStream stream, String filename) throws Exception {
         TikaConfig config = TikaConfig.getDefaultConfig();
         Detector detector = config.getDetector();

         TikaInputStream streams = TikaInputStream.get(stream);

         Metadata metadata = new Metadata();
         metadata.add(TikaCoreProperties.RESOURCE_NAME_KEY, filename);
         MediaType mediaType = null;
        mediaType = detector.detect(stream, metadata);
        return mediaType.toString();
    }

It give me this on the log :

java.lang.NoSuchMethodError: org.apache.commons.io.IOUtils.read(Ljava/io/InputStream;[B)I
    at org.apache.tika.detect.apple.BPListDetector.detect(BPListDetector.java:106)
    at org.apache.tika.detect.CompositeDetector.detect(CompositeDetector.java:85)
    at com.hraccess.helper.UserFileValidator.retrieveMimeType(UserFileValidator.java:313)
    at com.hraccess.webclient.servlets.ServletBlob.doPost(ServletBlob.java:429)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)

For this line :

at com.hraccess.helper.UserFileValidator.retrieveMimeType(UserFileValidator.java:313)

here is the line 313:

mediaType = detector.detect(stream, metadata);

So what can I do? It gives this error when I added the parsers to my pom.xml . How to find why?

peterh
  • 11,875
  • 18
  • 85
  • 108
Zahreddine Laidi
  • 560
  • 1
  • 7
  • 20
  • Is also [commons-io](https://mvnrepository.com/artifact/commons-io/commons-io/2.11.0) in your pom.xml? If I see it well, the apple `BPListDetector` needs it, although I have no idea, why it does not link it statically. – peterh Dec 01 '21 at 14:33
  • 1
    It says `NoSuchMethodError`, making likely that commons-io is in your pom.xml (at least indirectly, through a transient dependency). But this IOUtils has no `read` method. I think the most likely cause is that you are using an older commons-io which does not have this method yet. Use the latest, as I have shown in the link. – peterh Dec 01 '21 at 14:37

1 Answers1

4

The stacktrace says that org.apache.commons.io.IOUtils.read is missing. This is a static method in org.apache.commons.io.IOUtils, which class is part of the commons-io package.

  1. Make sure that commons-io is linked into your app (check the pom.xml, or an mvn dependency:tree output).

  2. Make sure it is linked in a relative newer version. V2.0 of the commons-io did not have this method yet (link), but the current one already has (link). In general, it is a good practice to use always the last stable version of everything.

peterh
  • 11,875
  • 18
  • 85
  • 108
  • i'm getting the same error, i have added the last commons-io dependency that you gived me on my pom.xml, and i still have the `java.lang.NoSuchMethodError: org.apache.commons.io.IOUtils.read(Ljava/io/InputStream;[B)I ` :( – Zahreddine Laidi Dec 01 '21 at 14:48
  • @ZahreddineLaidi Is commons-io in your `mvn dependencies:tree` output? If yes, with which version? – peterh Dec 01 '21 at 14:50
  • @ZahreddineLaidi The [source code](https://github.com/apache/tika/blob/main/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-apple-module/src/main/java/org/apache/tika/detect/apple/BPListDetector.java) of `BPListDetector` clearly shows that it wants to use `org.apache.commons.io.IOUtils.read`. Nothing special, it is a simple static import. **It must work**, 99% that you only have some trivial, easy-to-fix linking problem. Check the dependency tree and share the results with me. – peterh Dec 01 '21 at 14:54
  • Yes commons-io is 2.11.0, here is the line : `\- commons-io:commons-io:jar:2.11.0:compile` , and btw the mvn dependencies:tree doesn't work, so i used the mvn dependency:tree for this one.. – Zahreddine Laidi Dec 01 '21 at 14:58
  • @ZahreddineLaidi Right, `dependency:tree` is the correct my mistake! I fixed the answer. So it is there in the compile dependencies. Then it is likely not in the target jar. How do you run your app, do you create a fat jar? – peterh Dec 01 '21 at 15:00
  • This is a big project, so for this i created a project with my method and added the jar manually, and it still gives me the same error... (the jar are added on eclipse on the Build Path -> add externals jar..) @peterh – Zahreddine Laidi Dec 01 '21 at 15:02
  • @ZahreddineLaidi Right, but the result of your compiled app must be a .jar file in `target/`. Very likely, this jar file does not contain commons-io. Why, that is another big question but it does not contain. So first find out, where is your jar and how do you start it. Then check, if commons-io is there. In most cases, both your compiled app (target/something-1.2.jar) and also a fat app (target/something-fat-1.2.jar or so) is generated. *Only the second has the dependent libraries.* Do you use spring boot? – peterh Dec 01 '21 at 15:05
  • @ZahreddineLaidi An `mvn package` normally generates both jars, but if you run your maven over some IDE hook, it is often not generated. It depends a lot on the details of your project what you did not share. – peterh Dec 01 '21 at 15:07
  • On the big projet, on the target/ folder, on the lib folders, i found : `commons-io-2.1.jar`, is this the correct .jar ? – Zahreddine Laidi Dec 01 '21 at 15:08
  • @ZahreddineLaidi Yes, but it is in the lib folder and not in the target jar. Check your target jar, ideally the fat one. – peterh Dec 01 '21 at 15:48