我已经被困在这里太久了。我有一个只有一个依赖项的maven项目:
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
我正在使用IDE,它本身安装了所有必需的依赖项。我编写了一个使用net.sf.jasperreports
类的简单代码。为了创建一个包,我做了mvn package
,它创建了jar
。当我尝试将jar
作为java -jar myjar.jar
运行时,我收到一个错误说Caused by: java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JRDataSource
我只是无法理解我怎么能解决这个问题。 IDE能够找到类,但是从命令行调用时,它无法找到类。这是为什么?我该怎么办?
我甚至试图将jasperreports-6.8.0.jar
保持在/Library/Java/Extensions/
,但这也没有用。
以下是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.suhail</groupId>
<artifactId>JasperCSVDataSource</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.suhail.main.CommandLineRunner</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
</dependencies>
</project>
这是在我的项目中使用maven shade插件的部分,这当然与你的完全不同:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<artifactSet>
<includes>
<include>org.apache.poi:poi</include>
<include>org.apache.poi:poi-ooxml</include>
<include>org.apache.poi:poi-ooxml-schemas</include>
<include>org.apache.commons:commons-collections4</include>
<include>org.apache.xmlbeans:xmlbeans</include>
<include>org.apache.commons:commons-compress</include>
</includes>
<excludes>
<!-- Exclude main jar to avoid error 'Failed to create shaded artifact,
project main artifact does not exist' -->
<exclude>${project.groupId}:${project.artifactId}</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
请注意,这部分
pom.xml
进入<plugins> ... </plugins>
部分,在我的情况下,它在maven编译器插件下方和maven jar插件下面。您可以明确包含某些依赖项或明确排除某些依赖项。
结果将是目标文件夹中的2个文件,我个人使用具有原始名称的文件,另一个将具有由阴影或类似扩展的原始名称。
如果您在...中收到错误声明没有主要清单属性,那么请更改maven jar插件的配置,如下所示:
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
<mainClass>com.suhail.main.CommandLineRunner</mainClass>
</configuration>
使用Maven程序集插件代替Maven jar插件。
看到what-are-the-differences-between-maven-jar-plugin-and-maven-assembly-plugin!
Maven程序集插件创建一个完全可部署的包,其中包含所有依赖项。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.suhail.main.CommandLineRunner</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>