Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行maven构建jar文件时找不到记录器类_Java_Maven_Logging_Log4j - Fatal编程技术网

Java 运行maven构建jar文件时找不到记录器类

Java 运行maven构建jar文件时找不到记录器类,java,maven,logging,log4j,Java,Maven,Logging,Log4j,我正在尝试编写一个maven集成Java API。我已经包括了log4j用于日志记录。当运行eclipse时,它工作得很好,但是当maven包完成并且运行jar时,它无法使用java-jar\u name.jar从cmd行运行,并抛出一个错误 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger 现在log4j.properties文件放在src/main/resources文件

我正在尝试编写一个maven集成Java API。我已经包括了log4j用于日志记录。当运行eclipse时,它工作得很好,但是当maven包完成并且运行jar时,它无法使用java-jar\u name.jar从cmd行运行,并抛出一个错误

  Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
现在log4j.properties文件放在src/main/resources文件夹下。还提到了pom.xml。我试着寻找答案,但没有一个对我有用。 有什么帮助吗

           <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>Weather_Simulator</groupId>
     <artifactId>weather_simulator</artifactId>
     <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>weather_simulator</name>
 <url>http://maven.apache.org</url>

 <build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
     <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <mainClass>com.test.weather.simulator.MainClass</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>

  <plugin>
    <!-- NOTE: We don't need a groupId specification because the group is
         org.apache.maven.plugins ...which is assumed by default.
     -->
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
    </plugin>
</plugins>
  </build>

  <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

      <dependencies>
    <dependency>
            <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-configuration2</artifactId>
    <version>2.1.1</version>
</dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>


<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.6.1</version>
    <scope>test</scope>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

4.0.0
天气模拟器
天气模拟器
0.0.1-快照
罐子
天气模拟器
http://maven.apache.org
org.apache.maven.plugins
maven jar插件
3.0.2
真的
com.test.weather.simulator.MainClass
maven汇编插件
3.0.0
带有依赖项的jar
UTF-8
org.apache.maven.plugins
maven编译器插件
3.6.1
org.apache.commons
commons-configuration2
2.1.1
朱尼特
朱尼特
3.8.1
测试
org.apache.logging.log4j
log4j-slf4j-impl
2.6.1
测试
log4j
log4j
1.2.17
pom.xml中的
似乎有误。试试这个(注意我把“test”改为“compile”)


org.apache.logging.log4j
.pom.xml中的
似乎是错误的。试试这个(注意我把“test”改为“compile”)


org.apache.logging.log4j

这是一个类路径问题,Maven生成的jar只包含您的类。要解决这个问题,您可以在项目jar中打包所有依赖项:

这是一个类路径问题,Maven生成的jar只包含您的类。要解决这个问题,您可以将所有依赖项打包到您的项目jar中:

如果您想创建一个包含所有依赖jar的可执行jar,您需要注意两件事,您必须像以前一样使用maven assembly plugin,但是您忘记了将其绑定到生命周期中……如下所示:


[...]
[...]
maven汇编插件
3.0.0
带有依赖项的jar
组装
包裹
单一的
[...]
此外,将插件作为依赖项是完全错误的


org.apache.maven.plugins
maven编译器插件
3.6.1
这意味着从pom文件中删除此条目

用scope
test
定义一个依赖项意味着它只在单元测试期间可用,这意味着它永远不会被打包到结果jar文件中。这意味着您必须更改以下内容:


org.apache.logging.log4j
log4j-slf4j-impl
2.6.1
测试
具体如下:


org.apache.logging.log4j
log4j-slf4j-impl
2.6.1
解决这些问题后,您应该能够通过以下方式构建应用程序:

mvn clean package

并在名为like
weather\u simulator-1.0.0-SNAPSHOT-jar-with-dependencies.jar的目录中找到包含依赖项的结果jar文件,该文件应用于调用应用程序。

如果要创建包含所有依赖jar的可执行jar,有两件事需要注意您必须像以前一样使用maven assembly插件,但您忘记了将其绑定到生命周期…如下所示:


[...]
[...]
maven汇编插件
3.0.0
带有依赖项的jar
组装
包裹
单一的
[...]
此外,将插件作为依赖项是完全错误的


org.apache.maven.plugins
maven编译器插件
3.6.1
这意味着从pom文件中删除此条目

用scope
test
定义一个依赖项意味着它只在单元测试期间可用,这意味着它永远不会被打包到结果jar文件中。这意味着您必须更改以下内容:


org.apache.logging.log4j
log4j-slf4j-impl
2.6.1
测试
具体如下:


org.apache.logging.log4j
log4j-slf4j-impl
2.6.1
解决这些问题后,您应该能够通过以下方式构建应用程序:

mvn clean package

并在名为like
weather\u simulator-1.0.0-SNAPSHOT-jar-with-dependencies.jar的
target
目录中找到包含依赖项的结果jar文件,您应该使用该文件调用应用程序。

maven integrated API
是什么意思?生成的JAR是插件、库还是应用程序?对于库和插件,运行时和编译范围依赖项会自动包含在内,对于应用程序,您需要嵌入它们或自己提供它们。在
Java-jar
的情况下,您通常必须嵌入所有。maven集成API
意味着什么?生成的JAR是插件、库还是应用程序?对于库和插件,运行时和编译范围依赖项会自动包含在内,对于应用程序,您需要嵌入它们或自己提供它们。在
Java-jar
的情况下,通常必须嵌入所有。