Cannot Compile Spring Boot Project after Update to New Version - java

I updated my Spring Boot Application from 1.3.4 to 1.4.3 and refreshed the project using the Gradle Refresh. I now have an error in my main application class:
The project was not built since its build path is incomplete. Cannot
find the class file for java.lang.Object. Fix the build path then try
building this project BeverageDataServices Unknown Java Problem
The type java.lang.Object cannot be resolved. It is indirectly
referenced from required .class
files
BeverageDataServicesApplication.java /BeverageDataServices/src/main/java/com/boelter/beverage
Here is the main application class:
package com.xxx.beverage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableScheduling;
#SpringBootApplication (exclude = {DataSourceAutoConfiguration.class, MailSenderAutoConfiguration.class})
#ComponentScan(basePackages = "com.xxx.xxx")
#EntityScan("com.xxx.xxx.model")
#EnableScheduling
public class BeverageDataServicesApplication {
public static void main(String[] args) {
SpringApplication.run(BeverageDataServicesApplication.class, args);
}
}

It looks like the JRE was removed or not present in the Java Build Path ... thank you both for your quick insight!! I added the Java 8 JRE to the Build Path and it works... thanks

Related

Redundant declaration: #SpringBootApplication already applies given #ComponentScan

I created a spring boot application(3.0.1). Then I connected the Postgres database and executed it. The application is up on server 8080 smoothly. then I added below-mentioned annotations.
#EnableAutoConfiguration
#ComponentScan
whole file,
package com.example.SpringRecap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#SpringBootApplication
#EnableAutoConfiguration
#ComponentScan
public class SpringRecapApplication {
public static void main(String[] args) {
SpringApplication.run(SpringRecapApplication.class, args);
}
#GetMapping("/")
public void Employee() {
}
}
Now, below mentioned error are appeared,
Redundant declaration: #SpringBootApplication already applies #EnableAutoConfiguration
Redundant declaration: #SpringBootApplication already applies given #ComponentScan
Image of InteliJ:
If anyone knows the reason, please help me. If further information is needed to solve the problem please put a comment here.
Thank You.
below mentioned error are appeared: This means that you can remove #ComponentScan and #EnableAutoConfiguration.
As the documentation on spring mentioned, it is included in #SpringBootApplication:
Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their "application class". A single #SpringBootApplication annotation can be used to enable those three features, that is:
#EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
#ComponentScan: enable #Component scan on the package where the application is located (see the best practices)
#Configuration: allow to register extra beans in the context or import additional configuration classes
It is a check in the plugin of Intellij. Also see https://youtrack.jetbrains.com/issue/IDEA-177632

Run JUnit 5 Test Suite loading Spring context

I'm facing problems when running a Test Suite of JUnit 5 Jupiter with Spring.
I'm using latest version of Spring (5.2.8-RELEASE) (not spring-boot) and latest version of JUnit Jupiter (5.7.0).
Here is my Test Suite code:
package mypackage.test;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.suite.api.SelectPackages;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
import org.springframework.test.context.web.WebAppConfiguration;
#WebAppConfiguration
#ExtendWith(SpringExtension.class)
#ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = {
MyRootSpringConfiguration.class })
#SelectPackages("mypackage.test.units")
class MyTestSuite {
}
This way, I'm loading the context of the application, and then I want to execute all the corresponding tests.
But it seems not working, as Eclipse doesn't recognize this Class as JUnit 5 Test Class.
I have a similar configuration for the individual Test Classes, and they work fine. But each file loads the context, and that's not a good solution. I prefer to have a Suite that loads the context once, and then all the Tests uses this context to be executed.
Any idea about how to configure it?

While accessing the spring boot project via localhost, it throws 404 Not found error

I created a simple Spring-boot maven project with an added Tomcat v8.5 server to the project. The Tomcat server is started successfully, however while accessing the application via
http://localhost:8080 it throws a HTTP Status 404 – Not Found error. I have installed the jre 1.8 version. Please refer pom.xml for more details.
// FalconApplication.java
package com.qacoder.falcon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class FalconApplication {
public static void main(String[] args) {
SpringApplication.run(FalconApplication.class, args);
}
}
// TestController.java
package com.qacoder.falcon.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class TestController {
#RequestMapping("/")
public String home() {
return "Spring boot is working!";
}
}
Try to hit http://localhost:8080/ (Added extra slash) as you added / in your #RequestMapping.
Try http://localhost:8080/context/
context is your war file name.
In external tomcat, you put your war file, let say abc.war in webapp folder. Now start the tomcat server. The abc folder get created. That's the context. So run http://localhost:8080/abc/
It should work.
Does your application has web.xml or any class which implements WebApplicationInitializer. Do you have any filters configured in your application. If not check the web-app structure of the war file you have created. In your controller you have not specified the method Type(Get Request). Please the method type(#GetMapping instead of #RequestMapping) and try again.
If you wanna run the application without external tomcat then just run the class FalconApplication and hit the url - http://localhost/
or create a jar with mvn clean install if it is a maven project and run the jar file.

Spring boot 2. Java 10. JUnit. The package org.slf4j is accessible from more than one module: <unnamed>, slf4j.api

I use Maven, Java 10, Spring boot 2 and Junit 5 for create my application with unit tests.
Main application class:
package mypackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MyApplication {
private final static Logger LOGGER = LoggerFactory.getLogger(MyApplication.class);
public static void main(String[] args) {
LOGGER.info("I'am running...");
SpringApplication.run(MyApplication.class, args);
}
}
My JUnit test class:
package mypackage;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
#ExtendWith(SpringExtension.class)
#SpringBootTest(classes=MyTest.class)
public class MyTest {
private static final Logger LOGGER = LoggerFactory.getLogger(MyTest.class);
#Test
public void myTest() {
LOGGER.info("Message from test");
}
}
When i import my maven project in Eclipse 4.10, i get error on my test class:
The package org.slf4j is accessible from more than one module: <unnamed>, slf4j.api
But mvn install work fine and maven-surefire-plugin run correct. What i'm doing wrong? Or it's eclipse bug? Maybe Java 10, Junit and SLF4J not working together? in my module-info.java: requires slf4j.api;
Please help me.
I believe you've added the jar for slf4j to the ModulePath:
Go to Project -> BuildPath -> Config BuildPath -> Remove Jars from Modulepath
This happens when you have added the external jars in the ModulePath.
Remove those added external jars and add dependency using pom.xml
Check under Dependency Hierarchy, slf4j library should be under one dependency
Need remove module-info.class from your spring-boot project. spring-boot/#16031

Spring application does not start outside of a package

I am following this tutorial to build a basic application with Spring. It is working flawlessly as long as I follow this sub-directory structure:
└── src
└── main
└── java
└── hello
If I move my Application.java and ScheduledTasks.java classes out of the hello package I get the following error:
** WARNING ** : Your ApplicationContext is unlikely to start due to a `#ComponentScan` of the default package.
And a few seconds later, indeed...
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.context.annotation.AnnotationConfigApplicationContext#71fa8894: startup date [Wed Jan 18 22:19:12 CET 2017]; root of context hierarchy
My question is, why do I need to put my classes into a package? What use does it have? How can I avoid this error? Do I really need to use packages if it is a really simple application?
Put your java files again to hello package.
When a class doesn’t include a package declaration it is considered to be in the “default package”. The use of the “default package” is generally discouraged, and should be avoided.
It can cause particular problems for Spring Boot applications that use #ComponentScan, #EntityScan or #SpringBootApplication annotations, since every class from every jar, will be read.
Read more here.
I moved the class annotated with #SpringBootApplication from the default package to a specific package and it worked.
I had created a blank Maven folder where Java folder was the last one. I added class MainApplication.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
Further, I had to create a package within Java folder such as com.test and then I moved my MainApplication class into it. Note "package com.test;" Now this default package Spring boot is looking for.
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
Then it worked fine.

Categories