NoSuchMethod异常而创建和格式化.doc文件使用Apache POI-3.8测试版HWPF [英] NoSuchMethod exception while creating and formatting .doc file using Apache poi-3.8 beta hwpf

查看:332
本文介绍了NoSuchMethod异常而创建和格式化.doc文件使用Apache POI-3.8测试版HWPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行此code时,得到以下情况例外。


  

java.lang.NoSuchMethodError:org.apache.poi.POIDocument&下; INIT>


code段:

  {尝试    档案文件=新的文件(externalPath +/abc.doc);
    POIFSFileSystem FS =新POIFSFileSystem(新的FileInputStream(文件));
    HWPFDocument DOC =新HWPFDocument(FS);
    范围范围= doc.getRange();
    CharacterRun运行= range.insertAfter(的Hello World!);
    run.setFontSize(2 * 18);
    run.setBold(真);
    run.setItalic(真);
    run.setCapitalized(真);
    出的OutputStream =新的FileOutputStream(新文件(externalPath +/agnew.doc));
    doc.write(出);
    了out.flush();
    out.close();}赶上(例外前){
    Log.e(异常==,==+ ex.toString());
      ex.printStackTrace();
}

logcat的:

  LogCat中:致命异常:主营:java.lang.NoSuchMethodError:org.apache.poi.POIDocument。 :
在org.apache.poi.hwpf.HWPFDocumentCore(HWPFDocumentCore.java:145):
在org.apache.poi.hwpf.HWPFDocument(HWPFDocument.java:218):
在org.apache.poi.hwpf.HWPFDocument(HWPFDocument.java:186):
在com.vikas.prudent.CreateDocument.onCreate(CreateDocument.java:45):
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047):
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627):
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679):
在android.app.ActivityThread.access $ 2300(ActivityThread.java:125):
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033):
在android.os.Handler.dispatchMessage(Handler.java:99):
在android.os.Looper.loop(Looper.java:123):
在android.app.ActivityThread.main(ActivityThread.java:4627):
在java.lang.reflect.Method.invokeNative(本机方法):
在java.lang.reflect.Method.invoke(Method.java:521):
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868):
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626):
在dalvik.system.NativeStart.main(本机方法)


解决方案

这听起来像你有你的类路径的Apache POI的两个副本,而旧的和新的。我的直觉是,你HWPF罐子(便签)是新的,但它捡了一个​​旧的核心POI罐子,这就是为什么你要除外。

您需要做的是审查在classpath所有的罐子,并确定相关的POI罐子,那么是什么确保你有一个一致的人。

借助 POI常见问题解答具有的和入口对这个问题非常,与一些Java $ C $沿C,你可以用它来打印出哪个罐子的POI类来从。如果您无法直接发现错误的罐子,尝试移植像有显示你的Andr​​oid code中的code可以帮助您找到旧罐子。

I am getting following Exception when trying to run this code.

java.lang.NoSuchMethodError: org.apache.poi.POIDocument.< init >

Code Snippet:

try {

    File file = new File(externalPath + "/abc.doc");
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
    HWPFDocument doc = new HWPFDocument(fs);
    Range range = doc.getRange();
    CharacterRun run = range.insertAfter("Hello World!");
    run.setFontSize(2 * 18);
    run.setBold(true);
    run.setItalic(true);
    run.setCapitalized(true);
    OutputStream out = new FileOutputStream(new File(externalPath + "/agnew.doc"));
    doc.write(out);
    out.flush();
    out.close();

} catch (Exception ex) {
    Log.e("Exception==","=="+ex.toString());
      ex.printStackTrace();
}

Logcat:

Logcat : FATAL EXCEPTION: main : java.lang.NoSuchMethodError: org.apache.poi.POIDocument. : 
at org.apache.poi.hwpf.HWPFDocumentCore.(HWPFDocumentCore.java:145) : 
at org.apache.poi.hwpf.HWPFDocument.(HWPFDocument.java:218) : 
at org.apache.poi.hwpf.HWPFDocument.(HWPFDocument.java:186) : 
at com.vikas.prudent.CreateDocument.onCreate(CreateDocument.java:45) : 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) : 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) : 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) : 
at android.app.ActivityThread.access$2300(ActivityThread.java:125) : 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) : 
at android.os.Handler.dispatchMessage(Handler.java:99) : 
at android.os.Looper.loop(Looper.java:123) : 
at android.app.ActivityThread.main(ActivityThread.java:4627) : 
at java.lang.reflect.Method.invokeNative(Native Method) : 
at java.lang.reflect.Method.invoke(Method.java:521) : 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) : 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) : 
at dalvik.system.NativeStart.main(Native Method)

解决方案

It sounds like you have two copies of Apache POI on your classpath, and old one and a new one. My hunch is that your HWPF jar (Scratchpad) is new, but it's picking up an old core POI jar, which is why you're getting the exception.

What you need to do is review all the jars on your classpath, and identify the POI related jars, then ensure you have a consistent set of them.

The POI FAQ has and entry on this very problem, along with some Java code you can use to print out which jar the POI classes come from. If you can't spot the wrong jars directly, try porting something like the code shown there to your android code to help you find the older jar.

这篇关于NoSuchMethod异常而创建和格式化.doc文件使用Apache POI-3.8测试版HWPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆