Java 如何在 Maven 项目中处理 CLASSPATH?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20045110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:38:48  来源:igfitidea点击:

How to handle with CLASSPATH in Maven project?

javamavenclasspath

提问by user2148736

I am trying simple Maven app with Log4J ver 2-beta 9. In my pom.xml file I have these two dependencies (as is mentioned in Log4J Maven webpage):

我正在尝试使用 Log4J ver 2-beta 9 的简单 Maven 应用程序。在我的 pom.xml 文件中,我有这两个依赖项(如Log4J Maven 网页中所述):

<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.0-beta9</version>
</dependency>
<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0-beta9</version>
</dependency>

Eclipse sees Log4J library:

Eclipse 看到 Log4J 库:

enter image description here

在此处输入图片说明

But when I package the app and run it this Exception is thrown:

但是当我打包应用程序并运行它时,会抛出这个异常:

java -cp target/notification-1.0.0.jar com.example.Sandbox

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/LogManager
    at com.example.Sandbox.<clinit>(Sandbox.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.LogManager
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

I searched for this Exception and it seems to be related to CLASSPATH variable.

我搜索了这个异常,它似乎与 CLASSPATH 变量有关。

How should be CLASSPATH set for Maven project?

应该如何为 Maven 项目设置 CLASSPATH?

回答by Aaron Digulla

Maven is a build tool. It doesn't help you much runningthe final application.

Maven 是一个构建工具。它对您运行最终应用程序没有多大帮助。

You can use mvn exec:java -Dexec.mainClass="com.example.Sandbox"to run your app (see the question Maven Run Project) but this gets tedious when you have to pass arguments to it.

您可以使用mvn exec:java -Dexec.mainClass="com.example.Sandbox"来运行您的应用程序(请参阅问题Maven Run Project),但是当您必须将参数传递给它时,这会变得很乏味。

You can get the classpath that Maven used to compile your application with mvn dependency:build-classpath

您可以获得 Maven 用来编译您的应用程序的类路径 mvn dependency:build-classpath

That will print the classpath to the console. Note that it will be missing target/notification-1.0.0.jar

这会将类路径打印到控制台。请注意,它将丢失target/notification-1.0.0.jar

Another useful tool in this area is the assembly plugin; it will create one very big JAR with all the dependencies rolled into a single file when you specify the descriptor jar-with-dependencies.

这方面另一个有用的工具是程序集插件;当您指定描述符时,它将创建一个非常大的 JAR,将所有依赖项都卷入一个文件中jar-with-dependencies