有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javanet。旧金山。jasperreports。发动机JRuntimeException:无法加载以下字体

我的应用程序使用JasperReports生成PDF文件。我使用Assembly Maven插件为我的应用程序生成一个jar

问题是,当我使用命令行java -cp "PrintOutCore-1.0.jar" com.soprabanking.printout.Launcher运行jar时,会出现以下异常:

net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font: 
pdfFontName: DejaVu Sans
pdfEncoding: Identity-H
isPdfEmbedded : true
    at net.sf.jasperreports.engine.export.JRPdfExporter.getFont(JRPdfExporter.java:2176)
    at net.sf.jasperreports.engine.export.JRPdfExporter.getChunk(JRPdfExporter.java:1967)
    at net.sf.jasperreports.engine.export.JRPdfExporter.getPhrase(JRPdfExporter.java:1936)
    ...

但是,当我使用命令行java -cp "jasperreports-fonts-4.0.0.jar;PrintOutCore-1.0.jar" com.soprabanking.printout.Launcher将jasperreports字体jar添加到类路径时,问题消失了,一切正常

如何正确运行jar而不在命令行中手动将jasperreports字体jar添加到类路径

这是我的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>com.soprabanking</groupId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <artifactId>PrintOutCore</artifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.soprabanking</groupId>
            <artifactId>PrintOutDao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- BEGIN Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/xmlunit/xmlunit -->
        <dependency>
            <groupId>xmlunit</groupId>
            <artifactId>xmlunit</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3</version>
        </dependency>
        <!-- FINISH Junit -->
        <!-- START jasper dependencies -->
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.12</version>
        </dependency>
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>4.0.0</version>
        </dependency>
        <!-- END jasper dependencies -->
        <!-- Begin Apache POI -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10-FINAL</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10-FINAL</version>
        </dependency>
        <!-- FINISH Apache POI -->
        <!-- https://mvnrepository.com/artifact/net.contentobjects.jnotify/jnotify -->
        <dependency>
            <groupId>net.contentobjects.jnotify</groupId>
            <artifactId>jnotify</artifactId>
            <version>0.94</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <!-- Includes the runtime dependencies -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <skipTests>true</skipTests>
                    <includes>
                        <include>**/*Accountancy*.java</include>
                        <include>**/*AccountOfficer*.java</include>
                        <include>**/*AccountsWithout*.java</include>
                        <include>**/*AutomaticRev*.java</include>
                        <include>**/*GeneralSummary*.java</include>
                        <include>**/*GrandLivre*.java</include>
                        <include>**/*RevaluationOn*.java</include>
                        <include>**/*SyntheticGeneral*.java</include>
                        <include>**/*SyntheticBalance*.java</include>
                        <include>**/*BceaoBalance*.java</include>
                        <include>**/*DailyReport*.java</include>
                        <include>**/*DailyTransaction*.java</include>
                        <include>com.soprabanking.printout.test.utils.*.java</include>
                        <include>com.soprabanking.printout.service.reportcreators.xls.XLSUtilsTest.java</include>
                        <include>com.soprabanking.printout.test.service.ParserXmlUtilsTest.java</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

共 (1) 个答案

  1. # 1 楼答案

    经过长时间的搜索,我找到了问题的根源

    实际上,在打包时,程序集插件会找到文件jasperreports\u扩展名。属性两次,因此它删除一个,保留另一个

    解决方案是用Maven Shade插件替换Maven Assembly插件,并为其添加一个转换器

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                          <resource>jasperreports_extension.properties</resource>
                        </transformer>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>org.springframework.batch.core.launch.support.CommandLineJobRunner</mainClass>
                        </transformer>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>META-INF/spring.handlers</resource>
                        </transformer>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>META-INF/spring.schemas</resource>
                        </transformer>
                    </transformers>
                    <filters>
                        <filter>
                            <artifact>junit:junit</artifact>
                            <includes>
                                <include>junit/framework/**</include>
                                <include>org/junit/**</include>
                            </includes>
                            <excludes>
                                <exclude>org/junit/experimental/**</exclude>
                                <exclude>org/junit/runners/**</exclude>
                            </excludes>
                        </filter>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
            </execution>
        </executions>
    </plugin>