gpt4 book ai didi

java - 将 pdf 文件加载到已构建的 jar 中

转载 作者:行者123 更新时间:2023-12-02 05:20:54 25 4
gpt4 key购买 nike

我正在尝试从 pdf 中获取一些特定数据并将其保存在数据库中。这个想法是,在我的程序中,当您按下一个按钮时,使用 selenium,程序会下载 pdf 并将其上传到 jar 以便阅读。

我尝试了很多库来阅读 pdf,但所有库似乎都卡在同一个地方:当我尝试加载 pdf 时。

如果我在 IntelliJ 中编译该程序,它会完美运行,问题是当我将其编译为 .jar 时。加载时卡住并且不显示错误。尝试了我能做的一切,但没有任何效果。

这是我的代码现在的样子:

              try {

PdfReader reader = new PdfReader("out/artifacts/WAsNEWWINDOWS_jar/bills/facturaClaro.pdf");
System.out.println("This PDF has "+reader.getNumberOfPages()+" pages.");
String page = PdfTextExtractor.getTextFromPage(reader, 2);
System.out.println("Page Content:\n\n"+page+"\n\n");
System.out.println("Is this document tampered: "+reader.isTampered());
System.out.println("Is this document encrypted: "+reader.isEncrypted());
} catch (IOException e) {
e.printStackTrace();
}

这样的编码在 IntelliJ 中完美运行,当我将其编译为 .jar 时,我会像这样更改文件的位置:

PdfReader reader = new PdfReader("bills/facturaClaro.pdf");

任何贡献都可以帮助我。非常感谢!

编辑:我想澄清一下,目前我没有下载 pdf,我只是将 pdf 文件复制到与 .jar 相同的目录中。

更新:显然它抛出了异常:


java.lang.NoClassDefFoundError: com/itextpdf/text/pdf/PdfReader
at BillReader.readClaro(BillReader.java:195)
at WebScrapper.startBillReaderClaro(WebScrapper.java:467)
at Controller$20.call(Controller.java:4182)
at Controller$20.call(Controller.java:4179)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: com.itextpdf.text.pdf.PdfReader
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more

我正在寻找解决方案,但没有任何效果。请帮我!谢谢

编辑:顺便说一句,我的 pom.xml 看起来像这样,没有重复的依赖项

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>WAs</groupId>
<artifactId>seleWA</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<appName>SimManager</appName>
<vendor>YourCompany</vendor>
<mainClass>SimManager</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>sample.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java/excel</directory>
<includes>
<include>*.*</include>
</includes>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>
<dependencies>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.13.0</version>
</dependency>




<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>



<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>

<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.19</version>
</dependency>

<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-app</artifactId>
<version>2.0.15</version>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>com.liferay</groupId>
<artifactId>com.liferay.document.library.service</artifactId>
<version>1.0.1</version>
</dependency>


<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>5.0.8</version>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.1.1</version>
<scope>compile</scope>
</dependency>


<!--<dependency>
<groupId>com.github.Lamba92</groupId>
<artifactId>kresourceloader</artifactId>
</dependency>-->

<dependency>
<groupId>com.lynden</groupId>
<artifactId>GMapsFX</artifactId>
<version>2.10.0</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.6</version>
</dependency>




<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15</artifactId>
<version>1.44</version>
</dependency>

<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
<scope>compile</scope>
</dependency>


</dependencies>

</project>

project structure libraries libraries

最佳答案

该异常看起来像是您的 Maven 存储库中没有正确的依赖项。尝试查看两个 PdfReader 函数是否具有相同的依赖关系。就像你说的,你尝试了很多库,也许其中两个具有相同的名称函数,并且你以某种方式混合了它们。

关于java - 将 pdf 文件加载到已构建的 jar 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56262036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com