将pdf文件加载到已经建立的jar中

时间:2019-05-22 17:23:55

标签: java maven pdf jar

我正在尝试从pdf中获取一些特定数据并将其保存在数据库中。这个想法是,在我的程序中,当您按下带有硒的按钮时,该程序会下载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

2 个答案:

答案 0 :(得分:0)

该异常看起来像您在maven存储库中没有正确的依赖关系。尝试查看两个PdfReader函数是否具有相同的依赖关系。就像您说的那样,您尝试了太多的库,也许其中两个具有相同的名称功能,并且您以某种方式对其进行了混合。

答案 1 :(得分:0)

我可以解决!我只是使用以下命令分析了.jar:

jar tvf WAsNEWWINDOWS.jar

我意识到确实没有必要的课。正如我已经在IntelliJ中查看项目结构中的库一样,我查看了提取的工件,并且可以看到iText不存在,因此我将其添加为“提取目录”,并搜索iText .jar所在的位置它! 。再次编译jar,没有任何错误,它运行完美!我将图像保留在需要添加.jar的位置:

intellij extract directory