0

I'm now using poi-3.9 and I'm going to create an Android 6.0 app to read, write and create Excel files with Eclipse. I tried to do that with .xls but it's always read-only and required me to save a new file as .xlsx, so I'm now doing this with .xlsx.

In this case, as I know, I need poi.jar files to make my app work. So I put poi-ooxml-schemas-3.9, poi-3.9 and poi-ooxml-3.9 jar files into classpath. However, the problem is that it can compile the java with XSSFWorkbook imported but it cannot be found in runtime, and NoClassDefFoundError Occurs.

Here are the imports in java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

Here is the whole error message in LogCat.

E/AndroidRuntime(5363): FATAL EXCEPTION: main

E/AndroidRuntime(5363): Process: com.example.rentingmanager, PID: 5363

E/AndroidRuntime(5363): java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/poi/xssf/usermodel/XSSFWorkbook;

E/AndroidRuntime(5363):     at com.example.rentingmanager.NewItem.read(NewItem.java:97)

E/AndroidRuntime(5363):     at com.example.rentingmanager.NewItem.confirm(NewItem.java:323)

E/AndroidRuntime(5363):     at com.example.rentingmanager.NewItem.onClick(NewItem.java:333)

E/AndroidRuntime(5363):     at android.view.View.performClick(View.java:5201)

E/AndroidRuntime(5363):     at android.view.View$PerformClick.run(View.java:21209)

E/AndroidRuntime(5363):     at android.os.Handler.handleCallback(Handler.java:739)

E/AndroidRuntime(5363):     at android.os.Handler.dispatchMessage(Handler.java:95)

E/AndroidRuntime(5363):     at android.os.Looper.loop(Looper.java:148)

E/AndroidRuntime(5363):     at android.app.ActivityThread.main(ActivityThread.java:5525)

E/AndroidRuntime(5363):     at java.lang.reflect.Method.invoke(Native Method)

E/AndroidRuntime(5363):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)

E/AndroidRuntime(5363):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

E/AndroidRuntime(5363): Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.poi.xssf.usermodel.XSSFWorkbook" on path: DexPathList[[zip file "/data/app/com.example.rentingmanager-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.rentingmanager-1/lib/arm64, /vendor/lib64, /system/lib64]]

E/AndroidRuntime(5363):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)

E/AndroidRuntime(5363):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)

E/AndroidRuntime(5363):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)

E/AndroidRuntime(5363):     ... 12 more

E/AndroidRuntime(5363):     Suppressed: java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook

E/AndroidRuntime(5363):         at java.lang.Class.classForName(Native Method)

E/AndroidRuntime(5363):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)

E/AndroidRuntime(5363):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)

E/AndroidRuntime(5363):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)

E/AndroidRuntime(5363):         ... 13 more

E/AndroidRuntime(5363):     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

This is what I did to place the JARs in classpath

enter image description here

I would like to know why and the way to solve the problem. Any answers would be appreciated, thanks.

Ricky Ho
  • 68
  • 1
  • 11
  • 1
    Did you add the jars to the artifact you deliver? – questionaire Jul 18 '16 at 08:57
  • 1
    Well, normally, messages don't lie. If Android tells you that it can't find your classes, I guess: your statement "jars in classpath" must be wrong somehow. Because that is what the message is telling you: the classes you need are not available when Android starts searching for them. – GhostCat Jul 18 '16 at 08:57
  • JARs can be in the compile classpath, but missing from the produced artefact (the APK, in this case). You will need to add them to APK build instructions. – Tassos Bassoukos Jul 18 '16 at 08:58
  • Add it to your classpath – Hrabosch Jul 18 '16 at 08:58
  • Sorry for that I'm still lack of experience. How can I add them to APK build instructions? – Ricky Ho Jul 18 '16 at 13:56

2 Answers2

1

This is because of class found at compile time but not available at runtime Refer this link

Community
  • 1
  • 1
venkatesh gowda
  • 841
  • 2
  • 12
  • 26
  • thanks for your reply. I have tried to followed the link you posted. I removed the JARs and added them as variables, but the problem cannot be solved. I will try other possible solutions later, thanks again. – Ricky Ho Jul 18 '16 at 13:52
0

try this:

in your project go to Properties -> Java Build Path -> Order and Export, uncheck the option Android Dependencies, do a clean & Build project.

this is described in: Android ClassNotFoundException: Didn't find class on path

Community
  • 1
  • 1
Rohit Gupta
  • 443
  • 3
  • 17