错误:无法找到或加载主类引起:java.lang.ClassNotFoundException:

时间:2021-03-28 00:07:01

标签: java spring spring-boot maven

我目前正在开发 Spring boot CRUD 应用程序,并且在 docker 容器内从 .jar 运行它时遇到问题。它接下来返回:

Error: Could not find or load main class testgroup.private_clinic.SpringBootClass
Caused by: java.lang.ClassNotFoundException: testgroup.private_clinic.SpringBootClass

从 Intelij Idea 运行主类时没有这样的问题。我几乎可以肯定,主要问题在于 pom.xml 文件或不正确的项目结构,但我仍然无法修复它。

Project structure screenshot

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>testorg</groupId>
    <artifactId>private_clinic</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <dependencies>



        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.28.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.3.9.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.3.9.RELEASE</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.19</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.9.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>

        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.flywaydb/flyway-core -->
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>6.5.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin -->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
        </dependency>


    </dependencies>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <!-- The main class to start by executing java -jar -->
        <start-class>testgroup.private_clinic.SpringBootClass</start-class>
    </properties>

    <build>

        <finalName>private_clinic</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>testgroup.private_clinic.SpringBootClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.9.RELEASE</version>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>


    </build>


</project>

SpringBootClass:

package testgroup.private_clinic;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;


@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
public class SpringBootClass  {
    public static  void main(String[] args){
        SpringApplication.run(SpringBootClass.class, args);
    }
}

控制器类:

package testgroup.private_clinic.controller;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import testgroup.private_clinic.model.Patient;
import testgroup.private_clinic.service.PatientService;

import java.util.List;

@RestController
public class PatientController {

    PatientService patientService;
    private Patient patient;

    @Autowired
    public void setPatienceService(PatientService patientService){
        this.patientService = patientService;
    }

    @RequestMapping(value = "/api/v1/patient", method = RequestMethod.GET)
    public String allPatients(){
        Gson gson = new Gson();
        List<Patient> patients = patientService.allPatients();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("patients");
        modelAndView.addObject("patientList", patients);
        String output = gson.toJson(patients);
        return output;
    }

    @RequestMapping(value = "/api/v1/patient/{id}", method = RequestMethod.GET)
    public String getPatient(@PathVariable ("id") int id) {
        Gson gson = new Gson();
        patient = patientService.getByID(id);
        if (patient == null) {
//        List<Patient> patients = patientService.allPatients();
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("patients");
//        modelAndView.addObject("patientList", patients);
            return ("Error. Patient with id" + id + "does not exist");
        } else {
            String output = gson.toJson(patientService.getByID(id));
            return output;
        }
    }


    @PutMapping(value="api/v1/patient/{id}", consumes = "application/json", produces = "application/json")
    public String editPatient(@RequestBody Patient output, @PathVariable("id") int id ) {
        patient = patientService.getByID(id);
        if (patient == null) {
            return ("Error. Patient with id " + id + " does not exist");
        } else {
            Gson gson = new Gson();
            patient.setName(output.getName());
            patient.setSurname(output.getSurname());
            patient.setDiagnosis(output.getDiagnosis());
            patient.setPatronimic(output.getPatronimic());
            patient.setStatus(output.getStatus());
            patient.setAdress(output.getAdress());
            patientService.edit(patient);
            return ("Successfully edited patient with id" + patient.getId() + "new data" + gson.toJson(patient));
        }
    }

    @PostMapping(value = "/api/v1/patient", consumes = "application/json", produces = "application/json")
    public String addPatient(@RequestBody Patient patient) {
        patientService.add(patient);
        Gson gson = new Gson();
        String output = gson.toJson(patient.getId());
        return ("Created patient with id:" + output);
    }

    @DeleteMapping(value="/api/v1/patient/{id}", produces = "application/json")
    public String deleteFilm(@PathVariable("id") int id) {
        Gson gson = new Gson();
//        ModelAndView modelAndView = new ModelAndView();
        patient = patientService.getByID(id);
        if (patient == null) {
            return ("Error. Patient with id " + id + " does not exist");
        } else {
            patientService.delete(patient);
            return "Successfully deleted patient " + gson.toJson(patient);
        }
    }

Docker 文件:

FROM maven:3.6-openjdk-11-slim as builder
WORKDIR /opt/
COPY src pom.xml private_clinic.iml ./
RUN mkdir target \
    && mvn package \
    && ls -l target

FROM openjdk:11-jre-slim-buster as application
WORKDIR /opt/application/
COPY --from=builder /opt/target/private_clinic.jar ./
RUN useradd --create-home --user-group --uid "1001" "java" \
    && chown --silent --recursive java:java ./
EXPOSE 8080
USER java
CMD ["java", "-jar", "./private_clinic.jar"]

编辑 1:尝试将 SpringBootClass 移动到 config 文件夹,没有任何改变

.

1 个答案:

答案 0 :(得分:0)

您可以在 pom.xml 中提及主类的名称,如下所示

<properties>
    <start-class>testgroup.private_clinic.SpringBootClass</start-class>
</properties>