java - 错误: Could not find or load main class - When running a JAR at the prompt

标签 java spring-boot maven

我整个下午都在研究如何运行我正在开发的 Spring Boot 应用程序的 JAR。

通过mvn clean package命令生成JAR。 JAR 是在 target 文件夹内生成的,因此我在此文件夹中运行 CMD 并输入 java -jar apptest.jar 然后我得到

Error: Could not find or load main class br.com.myapp.Application

当我直接在 Eclipse 上运行该应用程序时,它运行得很好。

你能帮我吗?

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath /> 
    </parent>
    
    <groupId>br.com.braxxy.adm</groupId>
    <artifactId>brxmind</artifactId>
    <version>0.0.1</version>
    <name>brxmind</name>
    <description>Redmine like Applicaiton for Project Management</description>
    <packaging>${project.packaging}</packaging>
    <properties>
        <java.version>1.8</java.version>
        <start-class>br.com.braxxy.Application</start-class>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <type>jar</type>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </dependency>           
                        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>2.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <activatedProperties>dev</activatedProperties> 
                <project.packaging>jar</project.packaging>               
            </properties>            
            <build>
                <finalName>${project.artifactId}</finalName>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-help-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>show-profiles</id>
                                <phase>compile</phase>
                                <goals>
                                    <goal>active-profiles</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>            
                </plugins>
             </pluginManagement>
            </build>                              
        </profile>       

        <profile>
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
                <project.packaging>war</project.packaging>
            </properties>   
            <dependencies>
                <dependency>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-starter-tomcat</artifactId>
                   <scope>provided</scope>
                </dependency>
            </dependencies>  
            <build>
                <finalName>${project.artifactId}</finalName>
            </build>                       
        </profile>
    </profiles>

    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://repo.maven.apache.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
        </pluginRepository>
    </pluginRepositories>
    <repositories>
        <repository>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://repo.maven.apache.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

Application.java

package br.com.myapp;


import java.io.File;
import java.util.Locale;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(new Locale("pt", "BR"));
        return slr;
    }

    @Bean
    CommandLineRunner init(SystemRoleRepository systemRoleRepository, ProjectRoleRepository projectRoleRepository) {
        return args -> {
            for (ESystemRole role : ESystemRole.values()) {
                SystemRole systemRole = systemRoleRepository.findByRole(role.toString());
                if (systemRole == null) {
                    SystemRole newSystemRole = new SystemRole();
                    newSystemRole.setRole(role.toString());
                    systemRoleRepository.save(newSystemRole);
                }
            }
            
            for (EProjectRole role : EProjectRole.values()) {
                ProjectRole projectRole = projectRoleRepository.findByRole(role.toString());
                if (projectRole == null) {
                    ProjectRole newProjectRole = new ProjectRole();
                    newProjectRole.setRole(role.toString());
                    projectRoleRepository.save(newProjectRole);
                }
            }            
        };
    }
}

MANIFEST.MF

Manifest-Version: 1.0
Implementation-Title: myapp
Implementation-Version: 0.0.1
Build-Jdk-Spec: 1.8
Created-By: Maven Jar Plugin 3.2.0
Main-Class: br.com.myapp.Application

最佳答案

要使用 Spring Boot Maven 插件,请在 pom.xml 的插件部分中包含相应的 XML,如以下示例所示:

<project>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>getting-started</artifactId>
    <!-- ... -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

构建:

mvn clean package

运行:

java -jar target/brxmind.jar

输出:

...
2021-07-29 07:53:43.557  INFO 18739 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-07-29 07:53:43.576  INFO 18739 --- [           main] br.com.braxxy.Application                : Started Application in 2.112 seconds (JVM running for 2.476)

关于java - 错误: Could not find or load main class - When running a JAR at the prompt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68550489/

相关文章:

java - 在tomcat中部署后无法访问spring boot应用程序

java - @Transactional 无法在 Spring Boot 中与 CrudRepository 一起工作

java - Maven:以下 pom.xml 文件给出的空 jar 是什么?

java - 如何将对象从asynctask返回到android中的主类

java - 如何在 JSONObject 中保留 LinkedHashMap 排序?

java - Apache poi 表密码不起作用

maven - Maven 父级定义中的相对路径

java - Jersey 是否有可能访问注入(inject)的 HttpServletRequest,而不是代理

java - 如何在上下文创建错误后使用 spring-boot-starter-actuator 完成我的 spring-boot 应用程序?

java - 在 openjdk11 下执行 sonar-maven-plugin 时不支持的类文件主要版本 55