java - 无法在可部署的 .jar 中加载主类

标签 java maven

我正在尝试构建一个可执行 jar 并收到错误 Error: Could not find or load main class com.company.app.Main .我已经重新启动了我的电脑并尝试清理这个项目。我也试过使用 <addClasspath>true</addClasspath>没有运气......

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.company.app.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

最佳答案

$ tree executable-jar-with-dependencies/
executable-jar-with-dependencies/
|-- pom.xml
`-- src
    `-- main
        `-- java
            `-- com
                `-- stackexchange
                    `-- stackoverflow
                        `-- ExecutableJar.java

6 directories, 2 files

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>com.stackoverflow</groupId>
  <artifactId>question-19281476</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

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

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <!-- 
      I prefer to use the maven-shade-plugin because the final built artifact maintains the original name
      instead of appending "jar-with-dependencies" to the built artifact. But the maven-assembly-plugin works fine too.
      -->
      <!--
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-shade-plugin</artifactId>
              <version>2.1</version>
              <executions>
                <execution>
                  <phase>package</phase>
                  <goals><goal>shade</goal></goals>
                  <configuration>
                    <transformers>
                      <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.stackexchange.stackoverflow.ExecutableJar</mainClass>
                      </transformer>
                    </transformers>
                  </configuration>
                </execution>
              </executions>
            </plugin>
      -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>com.stackexchange.stackoverflow.ExecutableJar</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

可执行文件.java:

package com.stackexchange.stackoverflow;

public class ExecutableJar {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

现在从命令行执行以下命令可以证明这是可行的:

$ mvn clean package
$ java -jar target/question-19281476-1.0-SNAPSHOT-jar-with-dependencies.jar 
Hello World

关于java - 无法在可部署的 .jar 中加载主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19281476/

相关文章:

java - 如何在 activiti 中捕获定时器边界事件?

java - 编写高效代码和优化内存

java - JSON - 检查数组是否存在

maven - gradle 不是像 Maven 有 m2 那样有自己的仓库吗?

java - 是否可以有一个基于 "HotReload"docker 的 java 环境?

java - 如何创建通用的 FlatFileItemReader 来读取具有不同标题的 CSV 文件?

java - 线程进度条 fragment

java - 使用 Maven 模块化项目

java - 可以离线进行第一个 Maven 构建吗?

java - Mybatis源码项目构建报错: Non-resolvable parent POM