【问题标题】:Maven Java integrationMaven Java 集成
【发布时间】:2018-06-26 10:58:27
【问题描述】:

这是一个用 maven 构建的 simple Java code with SL4J。以下命令没有问题。他们都运作良好 mvn 编译 mvn包 mvn 干净安装 mvn exec:java

但是当我尝试以下任何一种方法时

java -cp target/MaventestApp-1.0-SNAPSHOT.jar com.maven.helloworld.App
jar tf ./target/MaventestApp-1.0-SNAPSHOT.jar

我收到以下错误

    exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at com.maven.helloworld.App.main(App.java:17)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

这是我的pom.xml 文件的相关部分:

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
  <slf4jVersion>1.6.1</slf4jVersion>
</properties>

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>${slf4jVersion}</version>
  </dependency>
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4jVersion}</version>
  </dependency>
</dependencies>

<build>
  <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.0.0</version>
      </plugin>
      <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.maven.helloworld.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.8.2</version>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>com.maven.helloworld.App</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

此示例的代码是here

有人可以帮助我更好地理解 maven 和执行过程吗?感谢您的时间。

【问题讨论】:

  • this 有帮助吗
  • 好吧,jar tf ... 不会执行任何操作,也不应该导致错误。您要么需要一个胖罐子,要么需要一个清单文件。您可能想退后一步,了解类路径的工作原理。
  • 普通的 java 命令根本不知道 Maven 有什么问题,所以你需要告诉它。现在最简单的方法是使用答案中建议的mvn exec:java 命令。

标签: java maven slf4j


【解决方案1】:

问题在于您如何构建 JAR 文件(其中不包含依赖项)。您可以改用maven-assembly-plugin(请参阅程序集和jar 插件here 之间的区别)。

基本上,将您当前的构建部分替换为以下内容:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.maven.helloworld.App</mainClass>
          </manifest>
        </archive>
        <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>

然后使用以下命令执行生成的 jar-with-dependencies:

java -jar target/MaventestApp-1.0-SNAPSHOT-jar-with-dependencies.jar

查看有关创建可执行 JAR 的更多信息here

【讨论】:

    【解决方案2】:

    Exec Maven Plugin 应该可以完成这项工作。类似的东西

    mvn exec:java -Dexec.mainClass=com.maven.helloworld.App
    

    实际的问题是您只将 jar 添加到类路径中,并且由于您自己的存档中没有 sl4j 类,但将它们声明为外部依赖项,因此它们很容易丢失(又名 NoClassDefFoundError)。

    上面的命令将所有运行时依赖项链接到 pom 中配置的类路径。

    【讨论】:

      猜你喜欢
      • 2020-03-04
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      • 2014-11-04
      • 2010-12-09
      相关资源
      最近更新 更多