Java Question [SOLVED]How to use lib inside other libs makes in Eclipse and resolve NoClassDefFoundError

The problem I'm encountering is the following:

- java.lang.NoClassDefFoundError: Failed resolution of: ... ;
- Caused by: java.lang.ClassNotFoundException: Didn't find class ...

First I followed the Libs creation tutorial, and I can use them normally within my project in B4A. However I need to use a second lib (file .jar) in my project, and I can't create instances of that referenced lib.

As I said earlier, both the .jar file and the project in the Eclipse IDE are working normally, but in B4A, it gives the error:
- NoClassDefFoundError !!

I keep thinking about the following question, I can use methods from other libs in my lib that I am creating, and use it in B4A, without presenting this error of not finding the referenced Class.

I really looked for everything here in the Forum related to this, but I was not successful in the searches.
 

DonManfred

Expert
Licensed User
Longtime User
You are not providing any useful information.
Where is the FULL Error?
Where is a link to the Project you are trying to implement?
How are you setting the Dependecies for the project?
What is your (java) wrapper Code?

When you get a ClassNotFoundException then you are missing adding a dependency.
 
Last edited:
Well, let's go.

First my goal is to use a second lib (file.jar) in the current lib that I am creating, so far so good, as I said earlier everything works both in the Eclipse IDE and in my created .jar file. In the first few lines of my original program I create an instance of that lib that I need to use.

package anywheresoftware.b4a;

import anywheresoftware.b4a.BA.ShortName;
import testeLib.PrintDays;

@ShortName("Days")
public class Days {

static PrintDays printDays = new PrintDays();

public Days() {
// TODO Auto-generated constructor stub
}

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Terca());
}
public String Segunda() {
return "Segunda-Feira";
}

public static String Terca() {
return PrintDays.Terca();
}

public String Quarta() {
return printDays.Quarta();
}

public String Quinta() {
return printDays.Quinta();
}

public String Sexta() {
return printDays.Quinta();
}
}



When trying to use in B4A, when I go to the Activity that uses that lib, the application breaks and shows the following ERROR.

java.lang.NoClassDefFoundError: Failed resolution of: LtesteLib/PrintDays;
Caused by: java.lang.ClassNotFoundException: Didn't find class "testeLib.PrintDays" on path: DexPathList[[zip file "/data/app/b4a.example-PxT5hY7ucGYgL6j9VqlFUg==/base.apk"],nativeLibraryDirectories=[/data/app/b4a.example-PxT5hY7ucGYgL6j9VqlFUg==/lib/arm, /system/lib, /vendor/lib]]


I found a 2014 publication similar to this issue ( NoClassDefFoundError Problem :( ) , where the only thing that is changed is the Java Compiler from 1.7 (in my case 1.8) to 1.6, however it still doesn't work. Whenever I use lib, this NoClassDefFoundError error appears and breaks the application.
 
You need to add @DependsOn(values={"yourJarfile"})

You can also add the dependency in the IDE with #AdditionalJar.

It worked, thank you very much Erel.
I'm a beginner at B4A, and I was already in this doubt for a few days.

Can you help me in the second topic ...
I'm trying to start a thread and get the current activity in an onCreate method below:

@override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
this.activity = this;

iGedi = new Gedi (this.activity);
iGedi = GEDI.getInstance (this.activity);
iPrint = iGedi.getPRNTR ();
try {
new Thread ();
Thread.sleep (250);
} catch (InterruptedException e) {
e.printStackTrace ();
}
}

However, this method is not being executed and so I can't get the Activity or start the Thread. Is there anything in B4A that is limiting this onCreate () method to be executed ?!

Or just trying to get Activity inside a lib doesn't work ?!
 
This is very bad Java code. Never use it.

Don't try to wrap an activity. Call these methods from the activity.

šŸ˜…
Actually, I am not very good at java at all.
I changed it and I'm using it now:

B4X:
public Days () {
    System.out.println ("87: Days"); //
    activity = (Activity) getActivity ();
    startIGEDI ();
}

private void startIGEDI () {
    System.out.println ("80: startGEDI ()");
    System.out.println ("81: activity:" + activity);
    run ();
}

@override
public void run () {
// TODO Auto-generated method stub
    iGedi = new Gedi (activity);
    iGedi = GEDI.getInstance (activity);
    iPrint = iGedi.getPRNTR ();
    
    try {
        new Thread ();
        Thread.sleep (250);
    }catch (InterruptedException e){
            e.printStackTrace ();
    }
}

private Activity getActivity () {
    // TODO Auto-generated method stub
    return this;
}

Apparently he prints an activity in my logcat, but inside B4A the following error message is shown:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext ()' on a null object reference

I know that this code must not be very good either, but my only goal here is to take the current activity and start the Thread, but this error is preventing me from that.
 
Last edited:
Top