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

Created on 5 Feb 2017  路  10Comments  路  Source: bytedeco/javacv

Hi I have seen all the issues similar to this and have tried updating all the dependencies to latest but still getting the same error.
<dependency><groupId>org.bytedeco</groupId><artifactId>javacv-platform</artifactId><version>LATEST</version></dependency>

I havealso added the below dependencies:
org.bytedeco.javacpp-presets:ffmpeg:3.2.1-1.3
and
org.bytedeco.javacpp-presets:opencv-platform:3.1.0-1.3

Below is my code:

     public static void main(String[] args) throws Exception {
     CanvasFrame frame = new CanvasFrame("ScreenCastingTest", 1.0);
     int x = 0, y = 0, w = 1024, h = 768; // specify the region of screen to grab
     FFmpegFrameGrabber grabber = //new FFmpegFrameGrabber(":0.0+" + x + "," + y);
             new FFmpegFrameGrabber("video=\"screen-capture-recorder\"");
     grabber        .setFormat("dshow");
    // grabber.setFormat("x11grab");
     grabber.setImageWidth(1024);
     grabber.setImageHeight(768);
     grabber.setFrameRate(10);
     grabber.start();

     while (frame.isVisible()) {
         frame.showImage(grabber.grab());
     }
     grabber.stop();
     grabber.close();
     frame.dispose();
 }

But I am getting below error:
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(Class.java:348)
at org.bytedeco.javacpp.Loader.load(Loader.java:585)
at org.bytedeco.javacpp.Loader.load(Loader.java:530)
at org.bytedeco.javacpp.avformat$AVFormatContext.(avformat.java:2819)
at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:468)
at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:462)
at opencv.OpenCvTest.main(OpenCvTest.java:18)

duplicate question

Most helpful comment

@saudet
Thanks Saudet finally found the solution. I read the ffmpeg documentation as per the new release the working configuration would be this.
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("desktop");
grabber.setFormat("gdigrab");

working like a charm now.

All 10 comments

Add the dependency for FFmpeg as well:
https://github.com/bytedeco/javacpp-presets/tree/master/ffmpeg#the-pomxml-build-file

@saudet
Hi Saudet I have added the dependecy: still getting same error

org.bytedeco
javacv-platform
LATEST

org.bytedeco.javacpp-presets
ffmpeg-platform
3.2.1-1.3

@saudet
As per the latest javacv dependency all the related dependencies gets downloaded as well. so when you asked to add ffmpeg-platform in pom I could see that the platform dependencies for ffmpeg were already in the classpath. Is there a possibility that a conflict between dependencies could cause this.

There can be conflicts, sure, so list only "javacv-platform" as dependency, that works for sure.

If you're on Windows, please follow these instructions and let me know what the output is:
https://github.com/bytedeco/javacpp-presets/wiki/Debugging-UnsatisfiedLinkError-on-Windows#using-dependency-walker

HI @saudet

Based on the 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(Class.java:348)
            at org.bytedeco.javacpp.Loader.load(Loader.java:585)

I tried this:

                 try {
            Loader.load(avutil.class);
            } catch (UnsatisfiedLinkError e) {
                 String path = Loader.cacheResource(avutil.class, "windows-  x86_64/jniavutil.dll").getPath();
                 new ProcessBuilder("C:/dependency_walker_x64/depends.exe", path).start().waitFor();
        }

and now the exception is null pointer

                Exception in thread "main" java.lang.NullPointerException
            at org.bytedeco.javacpp.Loader.cacheResource(Loader.java:331)
            at org.bytedeco.javacpp.Loader.cacheResource(Loader.java:315)
            at org.bytedeco.javacpp.Loader.cacheResource(Loader.java:311)
            at opencv.OpenCvTest.main(OpenCvTest.java:12)

I believe the native library "windows-x86_64/jniavutil.dll" is not present or I am passing a wrong library name to be loaded. So the resourceURL is null as the resource is absent or wrong i.e
return cacheResource(cls.getResource(name)); at loader.java :311 is having null value for resourceURL when calling cls.getResource(name);

So, yes, the DLL isn't found on your class path. You'll need to figure out how to include the correct JAR file in your class path for the build system you are using. For typical build systems, the "javacv-platform" will include everything, but it looks like you have something strange.

@saudet
I further checked all the dependencies I have the dependency jar
.m2\repository\org\bytedeco\javacpp-presets\ffmpeg\3.2.1-1.3\ffmpeg-3.2.1-1.3-windows-x86_64.jar which has the jniavutil.dll file. It is also present in maven classpath as it is part of javacv-playform.

I am not understanding why is it throwing null pointer then. Could you please confirm that the path "windows-x86_64/jniavutil.dll" is correct to load the dll.

Inorder to check the class path i removed all platform jars except windows x64 and added them manually into my build bath. Then i ran this code.
System.loadLibrary("jniavutil.dll"); or "windows-x86_64/jniavutil.dll"

for both I am getting no library defined in java.library.path.

Okay so now I have extracted the jar file for ffmpeg-3.2.1-1.3-library which has the jniavutil.dll file. Then I added to my JRE system native library. So the dll error is gone but when i run the code

        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(":0.0+" + x + "," + y);
         grabber.setFormat("x11grab");

or
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("video=\"screen-capture-recorder\"");
grabber.setFormat("dshow");

it throws exception at grabber.start() as couldnot find video is the set method called?

Exception in thread "main" org.bytedeco.javacv.FrameGrabber$Exception: av_find_input_format() error: Could not find input format "x11grab".
at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:486)
at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:462)
at opencv.OpenCvTest.main(OpenCvTest.java:25)

or

Exception in thread "main" org.bytedeco.javacv.FrameGrabber$Exception: avformat_open_input() error -5: Could not open input "video="screen-capture-recorder"". (Has setFormat() been called?)
at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:532)
at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:462)
at opencv.OpenCvTest.main(OpenCvTest.java:25)

@saudet
Thanks Saudet finally found the solution. I read the ffmpeg documentation as per the new release the working configuration would be this.
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("desktop");
grabber.setFormat("gdigrab");

working like a charm now.

@madie89 Great! Please consider making a contribution: issue #31

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chrisliu12345 picture chrisliu12345  路  4Comments

newstarbka picture newstarbka  路  5Comments

RaGreen picture RaGreen  路  4Comments

Bahramudin picture Bahramudin  路  3Comments

Maleandr picture Maleandr  路  3Comments