线程异常 "main" java.lang.NoClassDefFoundError 无法初始化 class org.bytedeco.javacpp.avutil

Exception in thread "main" java.lang.NoClassDefFoundError Could not initialize class org.bytedeco.javacpp.avutil

我有一个使用 bytedeco/javacv 库的 java 应用程序。

但是当我 运行 应用程序时,出现以下异常。

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:386)
    at org.bytedeco.javacpp.Loader.load(Loader.java:354)
    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.diyoron.ai.examples.VideoFrameProccessor.main(VideoFrameProccessor.java:38)
Error getting static method ID of org/bytedeco/javacpp/Loader/putMemberOffset

代码如下,

  String videoPath = video.getAbsolutePath();
  FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoPath);
  grabber.start();

根据 README.MD

中的以下声明

Additionally, we need to either set the javacpp.platform system property (via the -D command line option) to something like android-arm, or set the javacpp.platform.dependencies one to true to get all the binaries for Android, Linux, Mac OS X, and Windows. On build systems where this does not work, we need to add the platform-specific artifacts manually. For examples with Gradle and sbt, please refer to the README.md file of the JavaCPP Presets. Another option available for Scala users is sbt-javacv.

~参考GitHub.

我添加了参数 -Djavacpp.platform.dependencies="true",但我仍然遇到异常。我不确定是什么导致了这个问题,我在使用 C++ 包装器方面的经验很少。

PS: 我没有在我的机器上安装任何 c++ dependencies/libraries 或 open-cv。 所有的解决方案都与 Android 相关,而在我的 java 相关应用程序中,我并没有太多运气。

添加以下依赖项解决了问题。

        <!-- http://mvnrepository.com/artifact/org.bytedeco/javacv -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- http://mvnrepository.com/artifact/org.bytedeco/javacpp -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacpp</artifactId>
            <version>1.2.1</version>
        </dependency>


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

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