当前位置: 首页 > 知识库问答 >
问题:

错误:无法找到或加载主类-在提示符下运行JAR时

姜胤
2023-03-14

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

通过mvn清理包命令生成JAR。JAR是在目标文件夹中生成的,所以我在这个文件夹中运行CMD并键入java-jarapptest.jar

错误:无法找到或加载主类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

共有1个答案

太叔豪
2023-03-14

要使用Spring Boot Maven Plugin,请在您的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)
 类似资料:
  • 我在创建工作JAR文件时遇到问题。我可以跑步。类文件如下所示: 但作为jar运行(在构建脚本中)失败: 我将通过使用简单的Hello World项目来简化我遇到的问题: 文件需要进入文件夹。JAR文件需要在文件夹中。是构建JAR文件的bash脚本。如下: 你好世界java: 运行JAR文件,如构建中所示。sh结果 如前所述,直接运行应用程序效果很好。 这是的结构。 的内容。 我已经工作了6年了,现

  • 问题内容: 我想连接我的java程序以连接数据库并检索数据。它的编译完美,但运行时即时得到这个 我已经安装了Java SQL驱动程序,并将jar路径作为CLASSPATH添加到环境变量中 问题答案: 您应该从这里下载驱动程序 和JAR文件需要添加到项目类路径。 首先,右键单击Eclipse Project,然后单击Project-> Build Path-> Configure Build Pat

  • 当我正常运行此代码时,它会吐出: 异常线程"main"java.lang.ClassNotFoundExcture: bookReader 在java.net.URLClassLoader.find类(URLClassLoader.java:381) 在java.lang.ClassLoader.load类(ClassLoader.java:424)<在sun.misc.启动$AppClassLo

  • “错误:找不到或加载主类”。 我不知道我做错了什么。我有jre 1.8.0_45和jdk 1.7.0_71。

  • 问题内容: 我试图将HelloWorld放在.jar文件中并运行它,但是它不起作用。我创建了Java文件并在程序中键入内容,然后用cmd编写: 而且有效。然后我进入 得到了输出 然后我进入 第一行有效,而第二行却给我一个错误: 当我尝试使用64位jre7 \ bin,jdk1.7.0_51 \ bin,jdk1.7.0_51 \ jre \ bin中的java.exe文件打开它时,我得到的输出是(

  • 当我尝试在Windows上以编程方式运行命令时,会发生一些奇怪的事情。 运行以下命令时: 我知道这个错误 错误:找不到或加载主类JAR [106,97,118,97,32,-30岁,-128,-109,106,97,114,32,110,112,116,95,108,105,99,101,110,115,101,46,106,97,114,32,-30岁,-128,109,111,32,119,1