Kleber Mota
Kleber Mota

Reputation: 9095

spring-boot application fails to start

I recently start to develop a web application with spring-boot, anf, following the guide in the offical site, manage to create this two files:

pom.xml

<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>spring</groupId>
  <artifactId>app</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <start-class>com.spring.app.Application</start-class>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.8.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

Application.java

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

}

but when I try run the application with java -jar appname i get the error: Cannot find the main class: com.spring.app.Application. Program will exit, and in the terminal: Exception in thread "main" java.lang.NoClassDefFoundError: com/spring/app/Application.

What I am doing wrong?

Upvotes: 3

Views: 61783

Answers (4)

abhishek prasad
abhishek prasad

Reputation: 111

I was also getting same error. Its got resolved by converting the project to maven in eclipse as mentioned in below image:enter image description here

Hope it will you. :)

Upvotes: 0

lbt05
lbt05

Reputation: 49

you Application is in this package: package com.spring.app; which means your Application full path should be package com.spring.app.Application and your error code said com.spring.Application not found. Maybe you should check your run configuration

Upvotes: 0

Oleksandr Chalyi
Oleksandr Chalyi

Reputation: 86

Value of the following tag should be changed to match real class with main method. Try to specify correct package and run "mvn install"

<start-class>com.sample.Application</start-class>

Upvotes: 1

SteveS
SteveS

Reputation: 536

You have 2 things to do in your pom.xml.

First change the start-class to your application class. Second add the super-cool Spring Boot maven builder to your pom.

Something like this:

<?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>com.sample</groupId>
<artifactId>beanlist</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <start-class>com.sample.Application</start-class>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.8.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

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

</project

Then use "mvn install" to create your jar. Your code runs just fine.

Upvotes: 6

Related Questions