簡體   English   中英

無法在可部署的.jar中加載主類

[英]Unable to load main class in deployable .jar

我正在嘗試構建可執行jar並收到Error: Could not find or load main class com.company.app.Main 我重新啟動了PC,並嘗試對此項目進行清理。 我也嘗試過使用<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>

ExecutableJar.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

到目前為止, maven-shade-plugin是獲得胖可執行jar的最簡單解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM