0

So I need to use the jexcelApi for my Android project. After including it, I get no compiler error, but a runtime Exception NoClassDefFoundError on my device for every single class I use from that jar. Decompiling it shows that the needed classes are inside the jar.

I did following steps:

  1. Downloaded the jar
  2. Copied it to {Root_Folder}/libs
  3. compile fileTree(include: ['*.jar'], dir: 'libs') is in my gradle build script
  4. Called clean project
  5. Built the project

Full logcat entry:

05-11 23:28:05.234 26289-26289/? E/Assignment: Spreadsheet Error
java.lang.NoClassDefFoundError: jxl.write.WritableFont
at com.example.bene.assignment.spreadsheet.SpreadsheetController.<init>(SpreadsheetController.java:43)
at com.example.bene.assignment.spreadsheet.SpreadsheetController.create(SpreadsheetController.java:34)
at com.example.bene.assignment.MainActivity.onCreate(MainActivity.java:62)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
at android.app.ActivityThread.access$600(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4787)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)bs')

regards following code:

// create Formats
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
// Define the cell format
mTimes = new WritableCellFormat(times10pt);
// Lets automatically wrap the cells
mTimes.setWrap(true);

// create create a bold font with unterlines
WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false, UnderlineStyle.SINGLE);
mTimesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
mTimesBoldUnderline.setWrap(true);

I really don't know, how to proceed, please help me! Thank you in advance.

UPDATE

So I just found out, that some packages have got transitive dependencies which have to be included to compile them. So I found following links:

  • Maven repository
  • Sourceforge (cant post it because of stackoverflow restrictrions)

First saying that I'd need compile 'log4j:log4j:1.2.14', second saying that I'd need

compile 'org.slf4j:slf4j-api:1.7.2'
compile 'org.slf4j:slf4j-log4j12:1.7.2'

Following the Maven-link, I get the same result via

compile('net.sourceforge.jexcelapi:jxl:2.6.12') {
    transitive= true
}

which makes my dependency tree on that task look like following: Dependency Tree

Still getting the same Exception ...

be_bri
  • 61
  • 6

2 Answers2

1

You need to distinguish between ClassNotFoundException, which means the class is simply missing, and NoClassDefFoundError, which can mean a number of things, such as that the class is in the wrong package for the directory structure it was found in. Check that jxl.write.WritableFont is present in the JAR file as /jxl/write/WritableFont.class.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @be_bri That's not what I said. I said 'check that `jxl.write.WritableFont` is present in the JAR file *as `/jxl/write/WritableFont.class`*. – user207421 Aug 14 '17 at 10:12
-1

So I found my solution.

Although my problems magically disappeared after trying another lib and afterwards discarding those changes and trying again with jexcelApi, I found the solution here: About noClassDefFoundError.

Again about transitive dependencies: Your project depends on other libs which again depend on other libs. As gradle doesn't always get all transitive dependencies, you might need to look into the class that is throwing the Exception. In my version of Android studio it marks the imports as missing if the dependencies aren't added. Now that you know the missing imports, add the needed dependencies and you're done.

user207421
  • 305,947
  • 44
  • 307
  • 483
be_bri
  • 61
  • 6