3

I started to learn Spring Framework as my first ever app.I tried to get defined Beans count but i can't run it.

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class runDemo {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext();
        System.out.println(context.getBeanDefinitionCount());
    }
}

and my gradle file

buildscript {
ext {
    springBootVersion = '1.5.6.RELEASE'
}
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}


dependencies {

compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

and when i run this:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext ...

I have spring context and spring core in the External Libraries loaded.

EDIT:I upgraded to Intellij idea 2017.2 and it still give the same error

SemyColon.Me
  • 378
  • 3
  • 18
  • How do you run it? – Oleg Aug 06 '17 at 17:23
  • i just run the runDemo class ... i did as video tutorial but i don't know what's wrong now? – SemyColon.Me Aug 06 '17 at 17:25
  • 2
    Do you use an IDE? If this is your first ever app maybe start with something simpler? Like Hello World without Spring. – Oleg Aug 06 '17 at 17:45
  • i'm using java for two years ... im using intellij idea – SemyColon.Me Aug 06 '17 at 17:58
  • Ok, now we are getting somewhere. Because you wrote "Spring Framework as my first ever app" I thought that you just started programming. Check if [this](https://stackoverflow.com/questions/42587487/noclassdeffounderror-after-intellij-idea-upgrade) will help you. – Oleg Aug 06 '17 at 18:13
  • If you're using Java for 2 years, you should know better than naming your class like a method. Java class names conventionally begin with a capital letter. – Abhijit Sarkar Aug 06 '17 at 20:17

1 Answers1

0

i replaced

compile('org.springframework.boot:spring-boot-starter')

with

compile('org.springframework.boot:spring-boot-starter-web')
SemyColon.Me
  • 378
  • 3
  • 18