Android应用报错:未找到java.awt.Point类,PDF转DOC功能异常
Android PDF转DOC应用报错:找不到java.awt.Point类
问题背景
开发基于Java的Android PDF转DOC应用,使用Apache PDFBox 2.0.27和Apache POI 5.0.0依赖,执行PDDocument.load()时触发错误:
Didn't find class "java.awt.Point" on path: DexPathList[[dex file "/data/data/com.example.pdftodoc/code_cache/.overlay/base.apk/classes3.dex",
核心代码
转换函数
public void convertToWord(String inputPath, String outputPath){ // Toast.makeText(this, "Converteddddd"+outputPath, Toast.LENGTH_SHORT).show(); ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<Void> future = executorService.submit(() ->{ try { File inputFile = new File(inputPath); Log.d("path",inputPath); FileInputStream fis1 = new FileInputStream("/storage/emulated/0/XYZ.pdf"); Log.d("newPath", String.valueOf(fis1)); PDDocument pdf = PDDocument.load(fis1);// <---- 此处报错 Log.d("Load1","File Loaded"); PDFTextStripper stripper = new PDFTextStripper(); String text = stripper.getText(pdf); XWPFDocument doc = new XWPFDocument(); XWPFParagraph paragraph = doc.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(text); FileOutputStream fos = new FileOutputStream(outputPath); doc.write(fos); fos.close(); Log.d("Converted","Converted"); } catch (Exception e){ e.printStackTrace(); Log.e("Error", "convertToWord: "); } return null; }); try { future.get(); } catch (ExecutionException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } executorService.shutdown(); }
文件选择函数
public void openFile(View view){ Intent fileManager = new Intent(Intent.ACTION_GET_CONTENT); fileManager.addCategory(Intent.CATEGORY_OPENABLE); fileManager.setType("*/*"); startActivityForResult(fileManager,1); }
Activity结果处理
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 1 && resultCode == Activity.RESULT_OK){ if(data == null){ return; } Uri uri = data.getData(); String inputPath = getPath(uri); String outputPath = Environment.getExternalStorageDirectory().getPath() + "/output.docx"; path.setText("input Path "+inputPath + "Output Path" + outputPath); convertToWord(inputPath,outputPath); } } public String getPath(Uri uri){ String filePath = null; String[] projection = {MediaStore.Images.Media.DATA}; try(Cursor cursor = getContentResolver().query(uri,projection,null,null,null)) { if(cursor != null && cursor.moveToFirst()){ int colIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); filePath = cursor.getString(colIndex); } } catch (Exception e){ e.printStackTrace(); } if(filePath == null){ filePath = uri.getPath(); } return filePath; }
权限配置
String[] permission = {"android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE","android.permission.MANAGE_EXTERNAL_STORAGE"};
依赖配置
implementation 'org.apache.poi:poi:5.0.0' implementation 'org.apache.poi:poi-ooxml:5.0.0' // implementation 'org.apache.xmlbeans:xmlbeans:4.0.0' implementation 'com.github.virtuald:curvesapi:1.06' implementation 'org.apache.pdfbox:pdfbox:2.0.27'
解决方案
原因分析
Android系统不包含Java AWT库(java.awt.*属于桌面Java SE API),而Apache PDFBox的标准版本依赖AWT类,导致在Android上运行时找不到相关类。
修复步骤
替换PDFBox依赖为Android兼容版本
移除标准PDFBox依赖,改用专门适配Android的分支版本:// 移除原pdfbox依赖 // implementation 'org.apache.pdfbox:pdfbox:2.0.27' // 添加Android兼容版本 implementation 'com.tom_roush:pdfbox-android:2.0.27.0'该版本移除了对AWT和Swing的依赖,完全适配Android环境。
代码适配
保持原有逻辑不变,确保导入的是com.tom_roush.pdfbox.pdmodel.PDDocument等Android兼容包的类,而非原Apache PDFBox的包。优化文件读取逻辑
原转换函数中硬编码了固定文件路径,应替换为传入的inputPath参数,避免路径错误:// 替换硬编码路径 // FileInputStream fis1 = new FileInputStream("/storage/emulated/0/XYZ.pdf"); // 改为使用传入参数 FileInputStream fis1 = new FileInputStream(inputPath);
内容的提问来源于stack exchange,提问作者Tapan
相关产品推荐
相关产品推荐

