【问题标题】:Maven could not find or load main Class, after properly setting plugin正确设置插件后,Maven 无法找到或加载主类
【发布时间】:2018-01-25 00:53:40
【问题描述】:

我有一个问题,当我设置我的 pom.xml 时,我的应用程序仍然无法运行,说它找不到或加载主类。 我已经按照here

的说明设置了我的 pom.xml

我的 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.example</groupId>
    <artifactId>EditPropertiesFile</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.example.EditPropertiesFile.Main</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>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/commons-configuration/commons-configuration -->
        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.6</version>
        </dependency>
    </dependencies>

</project>

我使用命令 mvn clean compile assembly:single 打包应用程序,然后使用 java -jar outputedJar.jar

运行它

这就是那个罐子里的 MANIFEST.MF 说的:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: xxxx
Build-Jdk: 1.8.0_121
Main-Class: com.example.EditPropertiesFile.Main

但是,当我运行时,我得到下一个错误:

错误:无法找到或加载主类 com.example.EditPropertiesFile.Main

我不知道我还能尝试什么,因为我已经尝试过各种答案,而且它们似乎都解决了其他一些问题。

编辑: 运行命令时输出:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building EditPropertiesFile 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ EditPropertiesFile ---
[INFO] Deleting D:\Users\xxxx\Documents\Java_workspace\EditPropertiesFile\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ EditPropertiesFile ---
[WARNING] Using platform encoding (Cp1250 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Users\xxxx\Documents\Java_workspace\EditPropertiesFile\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ EditPropertiesFile ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-assembly-plugin:2.4:single (default-cli) @ EditPropertiesFile ---
[WARNING] Cannot include project artifact: com.example:EditPropertiesFile:jar:1.0; it doesn't have an associated file or directory.
[INFO] Building jar: D:\Users\xxxx\Documents\Java_workspace\EditPropertiesFile\target\EditPropertiesFile-1.0-jar-with-dependencies.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.525 s
[INFO] Finished at: 2017-08-16T21:17:56+02:00
[INFO] Final Memory: 13M/225M
[INFO] ------------------------------------------------------------------------

jar文件里面没有Main.java文件

我的项目结构:

【问题讨论】:

    标签: java maven jar manifest


    【解决方案1】:

    检查/提取jar文件,查看jar文件中是否添加了Main类,另外,检查包结构。

    另外,您可以在执行您提到的 mvn 命令期间添加来自 mvn 的输出日志吗?

    [更新] 看起来您的目录结构可能有问题,因此请检查是否正确。例如在这种情况下,如果 baseProjectDir 是您拥有 pom.xml (baseProjectDir\pom.xml) 的目录,那么您的 Main.java 应该位于相对于 baseProjectdir 的以下目录中: baseProjectDir\src\main\java\com\example\EditPropertiesFile\Main.java

    【讨论】:

    • 我现在编辑了这个问题。 Main.java 不在我输出的 jar 中。
    • 我不确定目录结构是否存在问题。我编辑了问题以在最后给出项目结构图片
    • 如果您从命令行使用 mvn,那么您可能需要确保目录结构符合 mvn 默认值。如果您还没有尝试过我提到的结构,也许您可​​以尝试一下。
    • 我直接使用 com/example/EditProp.. 而不是 main/java/com/.. - 感谢您的帮助!
    【解决方案2】:

    如果主类是 EditPropertiesFile 删除 .Main

    另外,添加到 pom.xml 中:

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.example.EditPropertiesFile.Main</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
    

    【讨论】:

    • 不,com.example.EditPropertiesFile 是包含主类 Main.java 的包。我的 pom 文件中已经有了所有这些(除了最终名称,这不是问题)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2015-06-04
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    相关资源
    最近更新 更多