英文:
NoClassDefFoundError as I run a Maven packaged JAR file
问题
我正在使用Maven从命令行编译和打包Java应用程序:
mvn clean
mvn compile
mvn package
这些命令可以正常运行。但是当我尝试运行生成的JAR文件时:
java -jar target/JarFile.jar
我遇到了以下错误:
Error: Unable to initialize main class com.company.Main
Caused by: java.lang.NoClassDefFoundError: com/google/api/client/json/JsonFactory
这是一个来自外部依赖的类。这些依赖项也由Maven管理(即,我没有在本地提供它们,而是在我的POM文件中列出它们)。由于我能够在集成开发环境(IDE)中运行应用程序,我认为我缺少一个使依赖项在JAR文件中可用的步骤。以下是我的POM文件:
<?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>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>test-maven-project</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.30.6</version>
</dependency>
<!-- other dependencies... -->
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.company.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
你可以指导我如何使依赖项在运行时可访问吗?
英文:
I am using Maven to compile and package a Java application from the command line:
mvn clean
mvn compile
mvn package
These commands run without a problem. But when I try to run the generated JAR file
java -jar target/JarFile.jar
I get the following error:
Error: Unable to initialize main class com.company.Main
Caused by: java.lang.NoClassDefFoundError: com/google/api/client/json/JsonFactory
Which is a class from an external dependency. These dependencies are also managed by Maven (i.e., I am not supplying them locally, but listing them in my POM file.) Since I am able to run the application inside the IDE, I assume I am missing a step to make the dependencies available to the JAR file. Here is my POM file:
<?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>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>test-maven-project</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.30.6</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.34.2</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.30.9</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>1.34.2</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-customsearch</artifactId>
<version>v1-rev20200401-1.30.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.company.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Could you point me in the right direction so that the dependencies are accessible at runtime?
答案1
得分: 1
运行jar包的命令行命令时,您必须在命令行中显式指定类路径,或者构建一个可执行的jar包:
链接:https://stackoverflow.com/q/574594/927493
顺便提一下:mvn package
命令已经会执行mvn compile
,因此您不需要显式调用mvn compile
命令。
英文:
To run your jar from commandline, you must either explicitly specify the classpath on the command line or build an executable jar:
https://stackoverflow.com/q/574594/927493
BTW: mvn package
already executes mvn compile
, so you need not call mvn compile
explicitly.
答案2
得分: -1
使用以下内容替换您的 pom.xml 文件,并重新构建项目:
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client-json</artifactId>
<version>1.1.0-alpha</version>
</dependency>
英文:
Use this in your pom.xml and rebuild your project.
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client-json</artifactId>
<version>1.1.0-alpha</version>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论