How to run Spring Boot quick start code - spring

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

Related

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 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?

Unable to start Jetty Server - Error scanning entry META-INF/versions/9/

When running XACML-PAP-ADMIN and XACML-PAP-REST on Windows 10. Java jdk1.8.0_144. I get next error:
Error scanning entry META-INF/versions/9/module-info.class from jar file:///D:/Projects/XACML/XACML-PAP-ADMIN/target/xacml-pap-admin-2.0.0-SNAPSHOT/WEB-INF/lib/log4j-api-2.11.0.jar
That could be linked to your version of Jetty, considering it fails on log4j 2.11 jar.
See this question:
log4j 2.9 and later are multi-release jars for Java 9.
Make sure to use a Jetty compatible with that, or use slf4j instead.
You can create a custom DevMode JettyLauncher:
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.dev.shell.jetty.JettyLauncher;
import org.eclipse.jetty.webapp.WebAppContext;
import java.io.File;
public class DevModeJettyLauncher extends JettyLauncher {
#Override
protected WebAppContext createWebAppContext(TreeLogger logger, File appRootDir) {
WebAppContext webAppContext = super.createWebAppContext(logger, appRootDir);
webAppContext.setAttribute("org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern", "none");//this is just a regex that matches nothing
return webAppContext;
}
}
And then configure it when launching.
Dev Mode Parameters: -server <package>.DevModeJettyLauncher

No qualifying bean of type org.springframework.jdbc.core.JdbcTemplate with Spring Boot & Batch

I have a strange problem. I made a Spring Batch application configurated and launched with Spring Boot. All my unit tests are passing.
But when I run program from command line :
java -cp "./batch-1.0-SNAPSHOT-jar-with-dependencies.jar:." com.batch.BatchApplication
I got a No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate].
If I run the same main class from Eclipse, all is working.
public class BatchApplication {
/**
* #param args
*/
public static void main(String[] args) throws Exception {
System.exit(SpringApplication.exit(SpringApplication.run(BatchConfiguration.class, args)));
}
}
Spring Boot "auto configuration" seems not working using command line.
Could you help me ?
Try java -jar your-long-name.jar (like in the docs and all the examples and guides). It uses a different main and makes the dependencies available at runtime.

Resources