org.springframework.boot.SpringApplication import throwing error - spring

I am working on this tutorial using Spring Tool Suites.
I've copied what it says and my code and pom look the same (copied for easy review)
package com.javatpoint.spring_boot_example_sts;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringBootExampleSts
{
public static void main(String[] args){
SpringApplication.run(SpringBootExampleSts.class, args);
}
}
My issue is that I am getting a red line under ' org.springframework.boot.SpringApplication. To my knowledge, this library is not deprecated and I can't seem to find any instance of it being moved.
Thanks in advance.

May be maven either maven dependency is not downloaded or your IDE has some issues.
You can try to restart your IDE.
If issue still persist, Please build using mvn clean package.

Related

Errors right after creating Spring Boot Application in IntelliJ

After creating the Spring Boot application in IntelliJ IDEA, it has errors. I didn't edit the code. It can't find org.spring.boot.SpringApplication. I've tried to create the app several times but no effect. All needed plugins are in the IDE, switched on. Maybe someone already had the problem
Errors in code
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);
}
}
Errors in console
Cannot resolve org.springframework.boot:spring-boot-autoconfigure:2.4.4
Cannot resolve jakarta.annotation:jakarta.annotation-api:1.3.5
Cannot resolve org.ow2.asm:asm:5.0.4
Cannot resolve org.junit.platform:junit-platform-engine:1.7.1
Cannot resolve com.fasterxml.jackson.core:jackson-core:2.11.4
Cannot resolve jakarta.xml.bind:jakarta.xml.bind-api:2.3.3
Cannot resolve org.apache.tomcat.embed:tomcat-embed-websocket:9.0.44
Cannot resolve com.fasterxml.jackson.core:jackson-databind:2.11.4
Cannot resolve org.glassfish:jakarta.el:3.0.3
Cannot resolve org.springframework.security:spring-security-web:5.4.5
Cannot resolve org.springframework.boot:spring-boot-test:2.4.4
Cannot resolve org.springframework:spring-aspects:5.3.5
Cannot resolve org.springframework:spring-web:5.3.5
Right click on the pom.xml and select Maven -> Reload Project

Spring Boot Application error: Exception in thread "main" java.lang.NoSuchMethodError

Why i'm getting this error while running simple SpringBoot Application.
Exception in thread "main" java.lang.NoSuchMethodError:
org.springframework.boot.SpringApplication.run(Ljava/lang/Class;[Ljava/lang/String;)Lorg/springframework/context/ConfigurableApplicationContext;
at
io.javabrain.springbootstarter.CourseApiApp.main(CourseApiApp.java:10)
package io.javabrain.springbootstarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class CourseApiApp {
public static void main(String[] args) {
SpringApplication.run(CourseApiApp.class, args);
}
}
It should be running successfully
For gradle project in gradle\wrapper\gradle-wrapper.properties file change distributionUrl variable to:
distributionUrl=https://services.gradle.org/distributions/gradle-4.7-bin.zip
If it is don't helps download project from https://start.spring.io/ and do the same.
If you use maven, just download maven project from https://start.spring.io/ . It should work fine.

Spring Boot #RunWith(SpringRunner.class) results in InitializationError

I have had this problem for a few days, checked similar questions on this forum and Googled in various ways but could not find an answer.
This is a Spring Boot starter project: I have a class Graphs which is annotated by #Component in a package under "src/main/java":
#Component
public class Graphs {
}
Then, I created test classes under "src/test/java". One of them (to test Graphs):
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.demo.Graphs;
#RunWith(SpringRunner.class)
#SpringBootTest
public class GraphsTest {
#Test
public void testRun () {
Graphs graph = new Graphs();
}
}
Then I ran testRun in Eclipse (right-click on "testRun"--> Run As --> Junit Test). The result I got was:
initializationError [Runner: Junit 4] (0.0001s)
The Failure Trace in Eclipse shows the following:
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testRun], {ExactMatcher:fDisplayName=testRun(com.example.demoTest.GraphsTest)], {LeadingIdentifierMatcher:fClassName=com.example.demoTest.GraphsTest,fLeadingIdentifier=testRun]] from org.junit.internal.requests.ClassRequest#1f7030a6
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:74)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
I made a few tweaks without any luck:
I bought my Eclipse Oxygen up-to-date, also tried in Eclipse 2018-12 edition, the same result.
If I remove #RunWith annotation and run testRun, it produced expected results.
I added a Public static void main and ran it as "Java Application", it produced expect results. I guess running it as a Java app bypassed #RunWith(SpringRunner.class).
Where did I do wrong?

org.springframework.boot.autoconfigure cannot be resolved in eclipse

I imported the spring-boot-1.1.10.RELEASE.jar into my j2ee project.
But i can not found the class of autoconfigure.
Here is code:
import org.springframework.boot.autoconfigure;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The Compile error:
The import org.springframework.boot.autoconfigure cannot be resolved
I had the same problem and I tried above solutions. But I have found a different issue.
In latest springframework, this org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder has been moved to
org.springframework.boot.jdbc.DataSourceBuilder. so you have to edit in import
And then you will notice that you have to use DataSourceBuilder.create(classLoader)
instead of new DataSourceBuilder(classLoader)
Go to your maven repository directory
For windows on below path
C:\Users\YourUser\.m2\repository\org\springframework\boot
Then delete spring-boot-autoconfigure folder.
Now go to eclipse and do maven update.
There is a copy/paste error - the code from the official documentation (https://spring.io/guides/gs/rest-service/) is:
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The code in the question is missing ".EnableAutoConfiguration" in the first line.
In case anyone else has this issue, I was able to solve this by deleting the spring-boot-autoconfigure artifacts from my local Maven repository and updating my project in Eclipse (which re-downloads the artifacts).
if you are using eclise then Right button on project -> Maven -> Update Project
If you are using IntelliJ IDEA Right click on project -> Maven -> Reimport
Also, try "Generate source and update folder" below the reimport if above method doesn't work. No need to delete any spring or maven folder or files.

Spring JUnit test getting NullPointerException with TestContext.retrieveContextLoaderClass(TestContext .java:197)

Using Helios, spring 3.0.5 (TestContext Framework) and JUnit 4.7. I am getting an initialization error indicating that it cannot find the ContextConfiguration. I ran ProcMon in the background and determined it is not apparently looking at all. I have tried the logical locations for the xml file to no avail. I am unclear of what I am doing incorrectly. Here is the code:
package com.hwcs.veri.agg.dao;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import com.hwcs.veri.jpa.License;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "/JpaIntegrationTests-context.xml" })
#TransactionConfiguration( transactionManager = "transactionManager",
defaultRollback = true )
#Transactional
public class JpaIntegrationTests
extends AbstractTransactionalJUnit4SpringContextTests
{
#Autowired
protected LicenseDao licenseDao;
#Test
public void getLicenses()
{
List<License> licenses = this.licenseDao.getLicenses();
assertEquals( "Expecting 1 license from the query",
super.countRowsInTable( "product_schema.license" ),
licenses.size() );
}
}
Is there some particular step that needs to be done to run this as a JUnit test inside Eclipse?
First and foremost, set the log level for org.springframework.test.context to DEBUG. That should tell you everything that the Spring TestContext Framework (TCF) is doing.
Note that with your above configuration, the TCF will attempt to load your application context from classpath:/JpaIntegrationTests-context.xml (i.e., in the root of your classpath). So make sure that the JpaIntegrationTests-context.xml file in fact exists in the root of the classpath (e.g., /src/test/resources/JpaIntegrationTests-context.xml for a Maven project layout). For the Maven project layout, you need to make sure that /src/test/resources is configured as a source folder in your IDE.
If this doesn't help you solve your problem, post the DEBUG output from the log.
Regards,
Sam (author of the Spring TestContext Framework)
Quoting Java Project: Failed to load ApplicationContext
"From the Sping-Documentation: A plain path, for example "context.xml", will be treated as a classpath resource from the same package in which the test class is defined. A path starting with a slash is treated as a fully qualified classpath location, for example "/org/example/config.xml".

Resources