简体   繁体   English

正确设置插件后,Maven无法找到或加载主类

[英]Maven could not find or load main Class, after properly setting plugin

I have a problem, when I set up my pom.xml, my application still wouldn't run, saying it could not find or load main Class. 我有一个问题,当我设置pom.xml时,我的应用程序仍然无法运行,并说找不到或加载主类。 I have setup my pom.xml as stated here 我已经按照此处所述设置了pom.xml

My 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>

I use command mvn clean compile assembly:single to package the application and then run it with java -jar outputedJar.jar 我使用命令mvn clean compile assembly:single打包应用程序,然后使用java -jar outputedJar.jar运行它

This is what MANIFEST.MF which is inside that jar, says: 这就是那个罐子里的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

However, when I run, i get next error: 但是,当我运行时,出现下一个错误:

Error: Could not find or load main class com.example.EditPropertiesFile.Main 错误:找不到或加载主类com.example.EditPropertiesFile.Main

I don't know what else I can try, as I have tried various answers all over SO and they all seem to fix some other issues. 我不知道还能尝试什么,因为我在SO上尝试了各种答案,它们似乎都解决了其他一些问题。

EDIT: output when running command: 编辑:运行命令时输出:

[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] ------------------------------------------------------------------------

Inside the jar file there is no file Main.java jar文件中没有文件Main.java

Structure of my project: 我的项目结构:

项目结构

Check/Extract jar file and look for the Main class is added in the jar file or not, Also, check the package structure. 检查/提取jar文件,并查找是否在jar文件中添加了Main类,此外,还检查了包结构。

Also, can you add the Output logs coming from mvn during the execution of mvn command you mentioned. 另外,您可以在执行上述mvn命令期间添加来自mvn的输出日志。

[ update ] Looks like your directory structure may be an issue, so check if thats correct or not. [ update ]看起来您的目录结构可能是一个问题,因此请检查是否正确。 For instance in this case if baseProjectDir is your directory where you have pom.xml (baseProjectDir\\pom.xml) then your Main.java should be residing in below directory relative to baseProjectdir: baseProjectDir\\src\\main\\java\\com\\example\\EditPropertiesFile\\Main.java 例如,在这种情况下,如果baseProjectDir是您存放pom.xml的目录(baseProjectDir \\ pom.xml),则Main.java应该驻留在相对于baseProjectdir的以下目录中:baseProjectDir \\ src \\ main \\ java \\ com \\ example \\ EditPropertiesFile \\ Main.java

If the main class is EditPropertiesFile remove .Main 如果主类是EditPropertiesFile,则删除.Main

also,add to pom.xml : 另外,添加到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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM