java.lang.ClassNotFoundException when trying basic JNI example



  • Hi,
    I am trying out a basic JNI example (Fibonacci example in https://conf.qtcon.org/system/attachments/132/original/QtCon16.pdf%3F1473147092) but I get the following build error:

    W System.err: java.lang.ClassNotFoundException: Didn't find class "com.example.test.JniTest" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
    W System.err: 	at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
    W System.err: 	at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    W System.err: 	at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    D libTestApp: (null):0 ((null)): Fibonacci:  0
    

    I have created the Java file JniTest.java as follows:

    public class JniTest
    {
        public static int fibonacci(int n)
        {
            if(n<0)
                return -1;
            else if(n==0 || n==1)
                return n;
    
            return fibonacci(n-1)+fibonacci(n-2);
        }
    }
    

    The java file is stored as android-source/src/com/example/test/JniTest.java and added to the Qt Creator project.

    In a .cpp file, I have:

    int AndroidInterface::fibonacci(int n)
    {
        return QAndroidJniObject::callStaticMethod<jint>("com/example/test/JniTest"
                                                     , "fibonacci"
                                                     , "(I)I"
                                                     , n);
    }
    

    and in my main.cpp:

      AndroidInterface android;
      ...
      qDebug() << "Fibonacci: " << android.fibonacci(3);
      ...
       return app.exec();
    

    However, this results in the errors shown above. Why?
    Tried it on Qt 5.10.1, on a virtual Android device.



  • @Diracsbracket if JniTest.java source code is completely displayed in the post, it looks to me that you're missing 'package' (see slide 34 from your reference):

    package com.example.test;
    


  • @Pablo-J.-Rogina said in java.lang.ClassNotFoundException when trying basic JNI example:

    it looks to me that you're missing 'package'

    You're absolutely right!
    This is a little bit embarrassing... "They have eyes, yet they don't see...".

    Thank you for your kind help, and for a good lesson learned. I'll never again forget
    to put the package declaration at the top of a Java file...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.