无法找到或加载主类:来自Maven的可执行JAR

时间:2018-04-09 08:11:40

标签: java maven

我正在尝试使用maven-assembly-plugin从Maven创建一个可执行的JAR。我的Jars正在创建,但没有我的源代码。我已经在POM中提到了单一目标并使用此命令创建了jar:" mvn clean package assembly:single"。但我仍然收到此错误:"错误:无法找到或加载主类com.som.bau.report.MainClass"。

enter image description here

请参阅下面我的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>VFIE_SOM_BAU_Report</groupId>
<artifactId>SOM_BAU_Report</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
    <jdk.version>1.7</jdk.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>false</downloadJavadocs>
            </configuration>
        </plugin>   
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.som.bau.report.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> 
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions> 
        </plugin>
    </plugins>
</build>

<dependencies>
<!-- Dependencies code goes here ---!>
</dependencies>

</project>

2 个答案:

答案 0 :(得分:1)

您应该将包装类型附加到根元素。

...
<modelVersion>4.0.0</modelVersion>
<groupId>VFIE_SOM_BAU_Report</groupId>  
<artifactId>SOM_BAU_Report</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
...

答案 1 :(得分:0)

我找到了解决方案。

需要在构建代码下添加此标记。

<sourceDirectory>src</sourceDirectory>