kotlin and spring boot error in Intellij - spring-boot

I'm creating a Kotlin project with Spring boot and Gradle project fromstart.spring.io
When importing this project to IntelliJ and run the project(Alt+Shift+F10) then show this error
Error: Could not find or load main class com.example.demo.DemoApplicationKt
mainclass :
package com.example.demo
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
#SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
SpringApplication.run(DemoApplication::class.java, *args)
}
How can I solve this problem?

You can run the application by clicking the run button(play button) which is on the left side of the main class as shown in below picture.
If you run like this, it will automatically create run configuration

Related

Unresolved reference: runApplication on Kotlin

When Im trying to import Spring kotlin archieve I have:
Unresolve reference: runApplication
The code is:
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
class RestapiApplication
fun main(args: Array<String>) {
runApplication<RestapiApplication>(*args)
Im still learning, so any help and advice is really appreciated.
I resolve this problem after trying second answer from here: https://stackguides.com/questions/31712046/kotlin-unresolved-reference-in-intellij
gradle clean build
Project Structure -> {Select Module} -> Kotlin.
In Project structure->Modules I have 2 main modules and 2 test modules. I delete excessed modules and all is workes now.

org.springframework.boot.SpringApplication import throwing error

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.

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?

How to run Spring Boot quick start code

I am new to Spring and Spring Boot, and I am trying it out. I am having trouble running the code sample from https://projects.spring.io/spring-boot/.
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
#Controller
#EnableAutoConfiguration
public class SampleController {
#RequestMapping("/")
#ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
I issued mvn install and everything appears to be fine. But then I issued java -cp target/myArtifId-1.0-SNAPSHOT.jar hello.SampleController and ClassNotFoundException is thrown.
How do I run this code sample?
As per spring boot documentation, you should be able to run your application with this command:
java -jar target/myArtifId-1.0-SNAPSHOT.jar
Spring Boot produces an executable jar, no need to specify a java class with a main method. That's also the reason why you can not include another class with a main method.
I prefer to use run goal of Spring Boot Maven Plugin to compile and run it in a single command:
mvn spring-boot:run

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