I am currently working through the example on scheduling tasks using Spring 4, Java 1.8, and Gradle (https://spring.io/guides/gs/scheduling-tasks/).
However, when running this example, I receive the following error:
Error:(11, 8) java: cannot access org.springframework.web.WebApplicationInitializer
class file for org.springframework.web.WebApplicationInitializer not found
The source code to my Application.java is as follows:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;
#SpringBootApplication
#EnableScheduling
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}
My gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
jar {
baseName = 'gs-scheduling-tasks'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter")
testCompile("org.springframework.boot:spring-boot-starter-test")
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}
Why is org.springframework.web.WebApplicationInitializer causing this error when it is not even mentioned in the guide?
Thanks.
Add this dependency to fix this problem. I perfectly worked for me.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
The guide does not extend SpringBootServletInitializer, remove it or add spring-boot-starter-web as a dependency if you want to start a Tomcat webserver inside you application.
You are extending SpringBootServletInitializer which implements WebApplicationInitializer
check http://grepcode.com/file/repo1.maven.org/maven2/org.springframework.boot/spring-boot/1.0.2.RELEASE/org/springframework/boot/context/web/SpringBootServletInitializer.java
Use this Application class instead as the guide is showing
#SpringBootApplication
#EnableScheduling
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}
Related
I created a new Spring application from https://start.srping.io
My build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.5'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '19'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
tasks.named('test') {
useJUnitPlatform()
}
File DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
I press button Run, the program error. How to fix it?
Looks like project dependencies mentioned in build.gradle file not downloaded properly because I can see some compilation errors as well.
Please run this command and try again.
gradle build
build.gradle
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
implementation("net.dv8tion:JDA:5.0.0-alpha.8")
}
test {
useJUnitPlatform()
}
bot.java
package Main;
import net.dv8tion.jda.api.JDABuilder;
;
public class testbotgradle {
public static void main(String[] args) throws LoginException {
JDABuilder jda = new JDABuilder.createDefault("my tocen");
jda.setActivity(Ativity.playing("test"));
jda.setStatus(OnlineStatus.ONLINE);
jda.build();
}
}
throws
java.lang.ClassNotFoundException: bot
Also seams like Java cant import the JDA package but I have no idea why. I have installed the dependencies. They are in the same project. Im using IntellijIDEA
It's probably a little late but for me the error can come from two places:
Either you did not launch your gradle.build after putting the code
Either the alpha 8 is no longer available and in this case
implementation("net.dv8tion:JDA:5.0.0-alpha.9")
I am new to spring-boot. I have created a simple spring-boot version 2.2.6 project with gradle build. I have created a welcome jsp just to print a header. I get 404 status when i run it on my server with http://localhost:8081/welcome.html Following is my build.gradle, application class, controller class and application.properties file:
//build.gradle
plugins {
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.banuprojects'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
implementation 'javax.servlet:jstl'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
//application class
package com.banuprojects.lmsdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class LmsdemoApplication {
public static void main(String[] args) {
SpringApplication.run(LmsdemoApplication.class, args);
}
}
//controller class
package com.banuprojects.lmsdemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class TestController {
#RequestMapping("/welcome.html")
public ModelAndView firstPage() {
return new ModelAndView("welcome");
}
}
//application.properties
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
I am not sure where I have one wrong with the implementation. Any help will be appreciated.
Thank you
As discussed over the comments. After seeing the application properties file, found that wrong port was passed in the URL.
No other technical error found.
I'm trying to deploy a war generated with springboot in Tomcat, in which I have succeeded previously, but can't figure out what is wrong this time.
The most important code is added:
config/AppConfig
#Configuration
#EnableTransactionManagement
#Component
//#EnableWebMvc
#ComponentScan("com.singleuseapps")
public class AppConfig {
LaptopsApplication
#SpringBootApplication
#ComponentScan(basePackageClasses = {AppConfig.class,LaptopsApplication.class})
public class LaptopsApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(LaptopsApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(LaptopsApplication.class, args);
}
build.gradle
buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'idea'
apply plugin: 'war'
springBoot{
executable = true
}
jar {
baseName = 'laptops'
}
war {
baseName = 'laptops'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile 'org.hibernate:hibernate-core:5.0.9.Final'
compile("com.h2database:h2")
testCompile('org.springframework.boot:spring-boot-starter-test')
compile 'javax.servlet:javax.servlet-api:3.1.0'
compile 'org.javassist:javassist:3.15.0-GA'
compile 'mysql:mysql-connector-java:5.1.31'
compile 'commons-dbcp:commons-dbcp:1.4'
compile('org.springframework.boot:spring-boot-starter-jdbc')
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
compileOnly "org.projectlombok:lombok:1.16.10"
compile "io.springfox:springfox-swagger2:2.6.0"
compile 'io.springfox:springfox-swagger-ui:2.6.0'
compile 'com.sun.mail:javax.mail'
compile( 'org.springframework.boot:spring-boot-starter-mail')
}
Hope someone gets why I failed.
[edit]
Line in catalina.out
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.singleuseapps.LaptopsApplication]; nested exception is java.io.FileNotFoundException: class path resource [com/singleuseapps/cart/TestMailController.class] cannot be opened because it does not exist
You should:
remove #Component from AppConfig (it's already annotated with #Configuration)
remove .configure method from LaptopsApplication, it's already annotated with #SpringBootApplication and is automatically a configuration class you don't need to add it again
Ensure your controller class is in the package specified by your two #ComponentScan annotations (you probably just need the one on your main app class)
I am new to Spring and i want to load some css and js into a view.
The static content is now in src/main/resources/static/... but when i navigate to localhost:8080/css/test.css it gives me a 404 error..
I don't use any xml configuration files.
My project structure:
Update: Application class
package com.exstodigital.photofactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Update: build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
group 'PTS4'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
testCompile("junit:junit")
}
Some controller (just testing stuff):
package com.exstodigital.photofactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
#Controller
//#Controller
public class MainController {
//#ResponseBody
#RequestMapping("/greeting/{name}/{hoi}")
public String greeting(#PathVariable("name") String name, #PathVariable("hoi") String hoi) {
//model.addAttribute("message", "BERICHT");
return name;
}
#RequestMapping("/test")
public String test() {
return "test";
}
}
Your structure is correct. Don't forget to make gradle clean task before run bootRun.