Java net.sf.jasperreports.engine.JRRuntimeException:无法加载以下字体

Java net.sf.jasperreports.engine.JRRuntimeException:无法加载以下字体,java,maven,jasper-reports,maven-assembly-plugin,Java,Maven,Jasper Reports,Maven Assembly Plugin,我的应用程序使用JasperReports生成PDF文件。我使用Assembly Maven插件为我的应用程序生成一个jar 问题是,当我使用命令行java-cp“PrintOutCore-1.0.jar”com.soprabanking.printout.Launcher运行jar时,我得到以下异常: net.sf.jasperreports.engine.JRRuntimeException:无法加载以下字体: PDF名称:DejaVu Sans pdf编码:Identity-H isPdf

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

问题是,当我使用命令行
java-cp“PrintOutCore-1.0.jar”com.soprabanking.printout.Launcher运行jar时,我得到以下异常:

net.sf.jasperreports.engine.JRRuntimeException:无法加载以下字体:
PDF名称:DejaVu Sans
pdf编码:Identity-H
isPdfEmbedded:正确
位于net.sf.jasperreports.engine.export.JRPdfExporter.getFont(JRPdfExporter.java:2176)
位于net.sf.jasperreports.engine.export.JRPdfExporter.getChunk(JRPdfExporter.java:1967)
位于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>

4.0.0
com.soprabanking
1
罐子
打印输出核
UTF-8
com.soprabanking
打印输出道
0.0.1-快照
朱尼特
朱尼特
4.9
org.hamcrest
汉克雷斯特岩芯
xmlunit
xmlunit
1.6
org.hamcrest
汉克雷斯特图书馆
1.3
net.sf.jasperreports
jasperreports
6.1.1
com.lowagie
文字
2.1.7
org.apache.poi
poi
3.12
net.sf.jasperreports
jasperreports字体
4.0.0
org.apache.poi
poi
3.10-决赛
org.apache.poi
poi ooxml
3.10-决赛
net.contentobjects.jnotify
通知
0.94
org.json
json
20090211
org.apache.maven.plugins
maven编译器插件
3.1
1.7
1.7
org.apache.maven.plugins
maven surefire插件
2.12.4
真的
**/*会计*.java
**/*会计官*.java
**/*AccountsWithout*.java
**/*AutomaticRev*.java
**/*GeneralSummary*.java
**/*GrandLivre*.java
**/*重新评估*.java
**/*通用*.java
**/*SyntheticBalance*.java
**/*BceaoBalance*.java
**/*DailReport*.java
**/*DailyTransaction*.java
com.soprabanking.printout.test.utils.*.java
com.soprabanking.printout.service.reportcreators.xls.XLSUtilsTest.java
com.soprabanking.printout.test.service.ParserXmlUtilsTest.java
maven汇编插件
2.6
带有依赖项的jar
组装
包裹
单一的

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

事实上,在打包时,程序集插件会两次找到文件jasperreports\u extension.properties,因此它会删除一个并保留另一个

一个解决方案是用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>

org.apache.maven.plugins
maven阴影插件
2.4.3
包裹
阴凉处
jasperreports_extension.properties
org.springframework.batch.core.launch.support.CommandLineJobRunner
META-INF/spring.handlers
META-INF/spring.schemas
junit:junit
junit/framework/**
org/junit/**
org/junit/experimental/**
org/junit/runners/**
*:*
META-INF/*.SF
META-INF/*.DSA
META-INF/*.RSA