147

I have a spring-boot application which I need to start up by going to the folder directory and start up my web application via command line. I have a class called Application.java and the code inside it is as followed.

@SpringBootApplication(scanBasePackages = {"com.ubs.tas.topcat.dashboard"})
public class Application extends SpringBootServletInitializer {
    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class.getName());
    private static final Class<Application> applicationClass = Application.class;

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

    public static void main(String[] args) {
        LOGGER.info("Starting...");
        SpringApplication.run(Application.class, args);
    }
}

I set up classpath then tried to run the command "java ApplicationUtility" but I'm getting this error message "Could not find the main class: ApplicationUtility. Program will exist."

Abdur Rahman
  • 1,420
  • 1
  • 21
  • 32
user4900074
  • 1,695
  • 4
  • 18
  • 24
  • 9
    What kind of project? For example, if you're using Maven with the appropriate plugin `mvn spring-boot:run` will work. Did you read [the docs](https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html)? – jonrsharpe Dec 15 '17 at 16:02

13 Answers13

221

I presume you are trying to compile the application and run it without using an IDE. I also presume you have maven installed and correctly added maven to your environment variable.

To install and add maven to environment variable visit Install maven if you are under a proxy check out add proxy to maven

Navigate to the root of the project via command line and execute the command

mvn spring-boot:run

The CLI will run your application on the configured port and you can access it just like you would if you start the app in an IDE.

Note: This will work only if you have maven added to your pom.xml

Adindu Stevens
  • 2,947
  • 3
  • 14
  • 20
  • what do you mean by "the root of the project" if the project is already built in a war archive ? – mounaim Mar 02 '20 at 19:37
  • 9
    The root of the the application is the folder that contains the pom.xml of the application is. It one folder backwards from where you found your the .war file. – Adindu Stevens Mar 03 '20 at 08:57
  • 2
    For projects where Spring Boot root POM is not used it does not work and we need to use full name as in this [Maven- No plugin found for prefix 'spring-boot' in the current project and in the plugin groups](https://stackoverflow.com/a/54873826/51591) answer. – Michał Ziober Jan 12 '21 at 15:47
  • 5
    Does it work "only if you have maven added to your pom.xml" or if you have spring-boot-maven-plugin installed in addition to maven itself? By the way what did you mean exactly by "having maven added to your pom.xml"? How are we supposed to add maven to pom.xml? – Andrey M. Stepanov Oct 25 '21 at 21:20
  • in a multi-module maven project you first have to change to the directory of the application project. When I run "mvn spring-boot:run" in the main folder of my project, I receive an error "no plugin found for prefix 'spring-boot' in the current project..." – Zaphod Beeblebrox Aug 30 '23 at 11:25
74

You will need to build the jar file first. Here is the syntax to run the main class from a jar file.

java -jar path/to/your/jarfile.jar fully.qualified.package.Application 
Srikanth Anusuri
  • 1,234
  • 9
  • 16
  • 3
    $ java -jar ./target/springboot-https-seed-1.0-SNAPSHOT.jar ./src/main/java/be/heydari/seed/Application no main manifest attribute, in ./target/springboot-https-seed-1.0-SNAPSHOT.jar – Philip Rego Aug 31 '19 at 18:15
  • @PhilipRego, try you are specifying the path to the java file, not the class. Try this instead java -jar ./target/springboot-https-seed-1.0-SNAPSHOT.jar be.heydari.seed.Application – Srikanth Anusuri Sep 06 '19 at 00:29
  • 1
    You can simplify that command by adding "fully qualified Main class name in pom.xml". Example: org.springframework.boot spring-boot-maven-plugin com.myproject.myapp.Application – Gana Nov 29 '21 at 08:26
  • java -jar path/to/your/jarfile.jar (after above pom changes) – Gana Nov 29 '21 at 08:27
67

If you're using gradle, you can use:

./gradlew bootRun
Paula T
  • 1,212
  • 11
  • 13
44

Run Spring Boot app using Maven

You can also use Maven plugin to run your Spring Boot app. Use the below example to run your Spring Boot app with Maven plugin:

mvn spring-boot:run

Run Spring Boot App with Gradle

And if you use Gradle you can run the Spring Boot app with the following command:

gradle bootRun
Abi Chhetri
  • 1,131
  • 10
  • 15
33

To run the spring-boot application, need to follow some step.

  1. Maven setup (ignore if already setup):

    a. Install maven from https://maven.apache.org/download.cgi

    b. Unzip maven and keep in C drive (you can keep any location. Path location will be changed accordingly).

    c. Set MAVEN_HOME in system variable. enter image description here

    d. Set path for maven

enter image description here

  1. Add Maven Plugin to POM.XML
    <build>
       <plugins>
         <plugin>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
  1. Build Spring Boot Project with Maven

        mvn package
    

    or

          mvn install / mvn clean install
    
  2. Run Spring Boot app using Maven:

         mvn spring-boot:run
    
  3. [optional] Run Spring Boot app with java -jar command

          java -jar target/mywebserviceapp-0.0.1-SNAPSHOT.jar
    
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
Abdur Rahman
  • 1,420
  • 1
  • 21
  • 32
25

1.Run Spring Boot app with java -jar command

To run your Spring Boot app from a command line in a Terminal window you can use java -jar command. This is provided your Spring Boot app was packaged as an executable jar file.

java -jar target/app-0.0.1-SNAPSHOT.jar

2.Run Spring Boot app using Maven

You can also use Maven plugin to run your Spring Boot app. Use the below command to run your Spring Boot app with Maven plugin:

mvn spring-boot:run

3.Run Spring Boot App with Gradle

And if you use Gradle you can run the Spring Boot app with the following command:

gradle bootRun
vikky
  • 77
  • 9
ngg
  • 1,493
  • 19
  • 14
19

Spring Boot provide the plugin with maven.

So you can go to your project directory and run

mvn spring-boot:run

This command line run will be easily when you're using spring-boot-devs-tool with auto reload/restart when you have changed you application.

soyphea
  • 469
  • 5
  • 11
  • 1
    For projects where Spring Boot root POM is not used it does not work and we need to use full name as in this [Maven- No plugin found for prefix 'spring-boot' in the current project and in the plugin groups](https://stackoverflow.com/a/54873826/51591) answer. – Michał Ziober Jan 12 '21 at 15:45
8

A Spring Boot project configured through Maven can be run using the following command from the project source folder

mvn spring-boot:run
Hariharan
  • 103
  • 1
  • 5
8

For macOS user If you configured your application using maven First, go to your project directory in the terminal then simply run

./mvnw spring-boot:run

You are done. Now you can hit localhost with port number.

This command mentioned in the spring official guide.

Vivek Lambhi
  • 81
  • 1
  • 2
4

One of the ways that you can run your spring-boot application from command line is as follows :

1) First go to your project directory in command line [where is your project located ?]

2) Then in the next step you have to create jar file for that, this can be done as

mvnw package [for WINDOWS OS ] or ./mvnw package [for MAC OS] , this will create jar file for our application.

3) jar file is created in the target sub-directory

4)Now go to target sub directory as jar was created inside of it , i.e cd target

5) Now run the jar file in there. Use command java -jar name.jar [ name is the name of your created jar file.]

and there you go , you are done . Now you can run project in browser,

 http://localhost:port_number
Badri Paudel
  • 1,365
  • 18
  • 19
4

For Maven based projects use this command to run the spring boot application:

mvn spring-boot:run
ahmnouira
  • 1,607
  • 13
  • 8
3

open cmd from your project path the then at cmd run following cmmands- 1st way to build and run->

mvn clean install

for run only ->

mvn spring-boot:run

1st way to build and run after jar file generated in target folder ->

java -jar target/accounts-0.0.1-SNAPSHOT.jar
0

I was having the same problem, I fixed it by modifying the dependency in the pom. I added web at the end.

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

<dependency>  
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
ZygD
  • 22,092
  • 39
  • 79
  • 102