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 楼答案
经过长时间的搜索,我找到了问题的根源
实际上,在打包时,程序集插件会找到文件jasperreports\u扩展名。属性两次,因此它删除一个,保留另一个
解决方案是用Maven Shade插件替换Maven Assembly插件,并为其添加一个转换器