0

I am trying to run my Spring Boot application through the command line but I am getting this error

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

pom File

<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

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 enter image description here

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?

Project structure

Updating 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>

Update pom errors

Fabio
  • 343
  • 1
  • 6
  • 17

1 Answers1

0

This will not work. Because If one class is dependent on another class that hasn't been compiled yet, the program won't run

  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

To run it as a JAR:

 java -jar target/your.jar
Mohit Sharma
  • 340
  • 2
  • 11
  • Not sure if I understood, so I must enter my project path and do javac *. java ? – Fabio Mar 04 '21 at 12:33
  • Updated my question with the project strucuture – Fabio Mar 04 '21 at 12:38
  • To run my project I should enter the commands `cd C:\Users\fabio\Desktop\MigrationsApp` `javac *.java` Right? – Fabio Mar 04 '21 at 12:39
  • No , You should run either the JAR or from IDE itself. Check Inside Target if there is a JAR. Paste it in any folder and then run java -jar target/your.jar . By doing javac *.java you are compiling it manually. – Mohit Sharma Mar 04 '21 at 12:51
  • I generated the jar and those erros disappeared but now I am getting `No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.` – Fabio Mar 04 '21 at 12:53
  • Try this . https://stackoverflow.com/questions/38792031/springboot-making-jar-files-no-auto-configuration-classes-found-in-meta-inf – Mohit Sharma Mar 04 '21 at 13:04
  • tried the solutin with the most upvotes but I am getting errors in the pom file(updated my question with them) – Fabio Mar 04 '21 at 15:14