简体   繁体   English

无法通过命令行运行 Spring 引导应用程序

[英]Can't run Spring Boot application trough the command line

I am trying to run my Spring Boot application through the command line but I am getting this error我正在尝试通过命令行运行我的 Spring 引导应用程序,但出现此错误

MigrationsApplication.java:3: error: package org.springframework.boot does not exist
import org.springframework.boot.SpringApplication;
                               ^
MigrationsApplication.java:4: error: package org.springframework.boot.autoconfigure does not exist
import org.springframework.boot.autoconfigure.SpringBootApplication;
                                             ^
MigrationsApplication.java:8: error: cannot find symbol
@SpringBootApplication
 ^
  symbol: class SpringBootApplication
MigrationsApplication.java:15: error: cannot find symbol
        SpringApplication.run(MigrationsApplication.class, args);
        ^
  symbol:   variable SpringApplication
  location: class MigrationsApplication
4 errors

when I try to build it using the command javac MigrationsApplication.java当我尝试使用命令javac MigrationsApplication.java构建它时

pom File pom文件

<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.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.migrations</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Migrations</name>
    <description>Demonstration of some migrations</description>
    <properties>
        <java.version>15</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>4.3.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Main Class主Class

package com.migrations.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;

@SpringBootApplication
public class MigrationsApplication {

    public static void main(String[] args) {


        //if entered the command run in the command line
        SpringApplication.run(MigrationsApplication.class, args);

        //if entered the command create in the command line
        /*MigrationGenerator generator=new MigrationGenerator();
        try {
            generator.generateMigrationFile("TEST2");
        } catch (IOException e) {
            System.out.println("There was an error generating the file");
        }*/
    }

}

I also have this "error " Select Run/Debug Configuration我也有这个“错误” Select 运行/调试配置在此处输入图像描述

How can I fix this?我怎样才能解决这个问题? Maybe I imported my project from start.io in a wrong way or is it because I don't have Maven installed?也许我以错误的方式从 start.io 导入了我的项目,还是因为我没有安装 Maven?

项目结构

Updating 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 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.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.migrations</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>MigrationsApp</name>
    <description>Migrations</description>
    <properties>
        <java.version>15</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>4.3.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.company.common</groupId>
            <artifactId>common-files</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <mainClass>${start-class}</mainClass>
                </configuration>

                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>migrations.MigrationsAppApplication</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>assemble-all</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

更新 pom 错误

This will not work.这行不通。 Because If one class is dependent on another class that hasn't been compiled yet, the program won't run因为如果一个 class 依赖于另一个尚未编译的 class,程序将无法运行

  javac *.java // compliles all java files in the dir

  java MigrationsApplication // runs the particular file

So you should compile all files before trying to run the program dependent on other files.因此,您应该在尝试运行依赖于其他文件的程序之前编译所有文件。

If your files are packaged, then something like this如果您的文件已打包,则类似这样

javac com.mypackage/.*java

java com.mypackage.MigrationsApplication 

For Error in your debug configuration, Check what is missing or wrongly provided in the configuration.对于调试配置中的错误,请检查配置中缺少或错误提供的内容。

Also, I suggest to create a sprint boot project and run it as a Spring boot JAR from the command line either or from the IDE itself另外,我建议创建一个 sprint 启动项目并将其作为 Spring 启动 JAR 从命令行或从 IDE 本身运行

To run it as a JAR:要将其作为 JAR 运行:

 java -jar target/your.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM