8

I am using Windows10, eclipse-neon with JDK1.8 version, I am getting the following exception.

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutil
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.bytedeco.javacpp.Loader.load(Loader.java:385)
at org.bytedeco.javacpp.Loader.load(Loader.java:353)
at org.bytedeco.javacpp.avformat$AVFormatContext.<clinit>(avformat.java:2719)
at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:391)
at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:385)
at com.segment.processor.AudioMain.main(ApacheMathAudioMain.java:20)
Error getting static method ID of org/bytedeco/javacpp/Loader/putMemberOffset

here are the dependencies I am using in my pom.xml

    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacv</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.bytedeco.javacpp-presets</groupId>
        <artifactId>ffmpeg</artifactId>
        <version>3.0.2-1.2</version>
    </dependency>

    <dependency>
        <groupId>org.bytedeco.javacpp-presets</groupId>
        <artifactId>opencv</artifactId>
        <version>3.1.0-1.2</version>
    </dependency>

    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacpp</artifactId>
        <version>0.10</version>
    </dependency>
Sat
  • 3,520
  • 9
  • 39
  • 66
  • I recommended to use OpenCV which has a good Java wrapper instead of JavaCV. – Bahramdun Adil Mar 16 '17 at 09:31
  • Check the jar files to see whether they contain the class e.g jar tf [jarfile] | grep org.bytedeco.javacpp.avutil – William Greenly Mar 16 '17 at 09:36
  • removed JavaCV and used OpenCV dependency but still getting the same exception – Sat Mar 16 '17 at 09:37
  • I have opened all the available jars like `javacpp-0.10, javacpp-1.2.2, opencv-3.1.0-1.2, javacv-1.1` etc but none of them contains `avutil` class. Where it is available actually.. I mean in which dependency – Sat Mar 16 '17 at 09:45
  • update this dependency to org.bytedeco javacpp 0.10 1.4.1. – Prasad Apr 19 '18 at 10:27
  • It would be good if you'd include a minimal and complete `pom.xml` and `main` class that reproduces this error. There are some issues with the dependencies in your pom (see my answer), but without a complete example we are all basically fishing in the dark, as there could be more issues with your project. – Max Vollmer Apr 19 '18 at 12:41

4 Answers4

15

First problem

Your versions don't match. org.bytedeco.javacpp in version 0.10 is from Dec 2014, while all your other versions are from May 2016. You need to use version 1.2 of org.bytedeco.javacpp, or better yet, update all dependencies to the latest version.

You can see the versions here:

org.bytedeco.javacpp-presets » opencv

org.bytedeco.javacpp-presets » ffmpeg

org.bytedeco » javacv

org.bytedeco » javacpp

Second problem

You include the dependencies for Java code only, but you don't include the dependencies for native code (both opencv and ffmpeg are native libraries). You need to include opencv-platform and ffmpeg-platform instead:

<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacpp</artifactId>
    <version>1.4.1</version>
</dependency>
<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv</artifactId>
    <version>1.4.1</version>
</dependency>
<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>opencv-platform</artifactId>
    <version>3.4.1-1.4.1</version>
</dependency>
<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>ffmpeg-platform</artifactId>
    <version>3.4.2-1.4.1</version>
</dependency>

This will make Maven download and include opencv and ffmpeg libraries for Android, Linux, MacOS and Windows, both x86 and x64.

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
2

java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutil

Actual cause of error is different versions of dependencies. That's why javacpp package failed during mapping of classes.

Follow these step to resolve this problem:

  • Download latest version of Javacv library package from here
  • Copy these three .jar files into libs folder

    • ffmpeg.jar
    • javacv.jar
    • javacpp.jar
  • Create jniLibs folder in app\src\main

  • Now, create four different folders for different architectures

    • arm64-v8a
    • armeabi
    • armeabi-v7a
    • x86
  • Change extension of these two files ffmpeg-android-arm.jar, ffmpeg-android-x86.jar to .zip then unzip both folders and Copy .so files for each architecture and paste in its respected directory. Your resultant directory should be look like this

  • List item

  • Add .jar dependencies in your gradle file as follows:

    implementation files('libs/ffmpeg.jar')
    implementation files('libs/javacpp.jar')
    implementation files('libs/javacv.jar')```
    

Thanks for reading :)

Rahul Gupta
  • 116
  • 6
0

This means that the class is there at compiletime but missing at runtime. You have a couple of options:

  • Always execute with a Maven plugin.

  • Include the library in the classpath when running.

  • Use the Maven Shade plugin to make an Uber-Jar which includes dependencies.

I hope this helps!

Caveman
  • 2,527
  • 1
  • 17
  • 18
  • I am sorry for downvoting your answer, as I don't want to discourage a new user from contributing to the platform, but in this particular case your answer is unfortunately wrong. The Java class is definitely there, it just can't be initialized by the JVM because it relies on native code, and that native code wasn't included as dependency. – Max Vollmer Apr 25 '18 at 21:19
  • Thank you for correcting me! How did you know that the dependency relied on native code? – Caveman Apr 26 '18 at 12:50
  • Because it does? It's a direct wrapper of native code. Just look at the [source](https://github.com/bytedeco/javacpp-presets/blob/master/ffmpeg/src/main/java/org/bytedeco/javacpp/avutil.java). – Max Vollmer Apr 26 '18 at 12:54