代码之家  ›  专栏  ›  技术社区  ›  riorio

“错误:无法在Spring Boot的模块中找到或加载主类”

  •  0
  • riorio  · 技术社区  · 7 年前

    我有一个 Spring Boot 项目中模块中的应用程序。

    该模块有自己的pom。xml和外部项目有自己的 pom.xml

    模块的 波姆。xml <mainClass>...</mainClass> 指向应用程序主类的注释。

    它还引用了父对象 波姆。xml <parent>...</parent> 属性,指向父对象 artifactId, groupId & version .

    在Intellij中运行应用程序时,它运行正常。

    问题是当尝试从jar运行应用程序时:

     java -jar myApp.jar
    

    然后我们得到

    错误:无法找到或加载主类com。实例服务器应用

    我们错过了什么?

    编辑-添加POM文件:

    父pom文件:

    <?xml version="1.0" encoding="UTF-8"?>
      <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>my-app</artifactId>
    <packaging>pom</packaging>
    <version>5.1-SNAPSHOT</version>
    <modules>
        <module>my-app-server</module>
    </modules>
    
    <parent>
        <groupId>com.example</groupId>
        <artifactId>my-parent</artifactId>
        <version>LATEST</version>
    </parent>
    
    <repositories>
     ...
    </repositories>
    
    
    <name>...</name>
    <description>...</description>
    
    
    <properties>
     ...
    </properties>
    

    模块pom文件:

    <?xml version="1.0" encoding="UTF-8"?>
     <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">
    <parent>
        <artifactId>my-app</artifactId>
        <groupId>com.example</groupId>
        <version>5.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example.my-app</groupId>
    <artifactId>my-app-server</artifactId>
    
    <repositories>
      ...
    </repositories>
    
    
    <dependencies>
    ...
    
    </dependencies>
    
    <build>
        <plugins>
    
    
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <optimize>true</optimize>
                    <debug>true</debug>
                    <forceJavacCompilerUse>true</forceJavacCompilerUse>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
    
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.example.server.Application</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot-version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>assembly</id>
                        <phase>package</phase>
    
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <tarLongFileMode>posix</tarLongFileMode>
                            <descriptors>
                                <descriptor>package.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
        </plugins>
    </build>
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   Pavan Sista    7 年前

    使用winzip或winrar打开jar文件,如果您使用的是linux,请使用archive manager。然后转到META-INF,打开清单。并确保属性“Main Class:your.Class.path”是正确的。

        2
  •  0
  •   lestherxm    4 年前

    我也有同样的错误,但在一个使用javafx的简单java项目中,我已经构建了这个项目,出于某种原因,我想更改它找到主类的包的名称,当我运行程序时,我得到了这个错误。我解决它:

    1. 修改引用主类的文件“module info.java”

    2. 我还修改了pom。xml

      <configuration><mainClass>pathToTheMainClass</mainClass></configuration>
      

    在这里,您必须参考包含主类的包