How Java know where is the dependency jar? - java

I've made a simple maven project, here is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>HelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HelloWorld</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
</dependencies>
</project>
It is a single file project, so this is the code:
package com.example;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class App {
public static void main (String[] args) {
System.out.println("Hello World!");
}
}
I run mvn package and then
java -cp target/HelloWorld-1.0-SNAPSHOT.jar com.example.App and everything works, i see "Hello World!" in my console.
So here's my question: how does Java know where is org.springframework.boot.autoconfigure.SpringBootApplication if i didn't specified the jar containing this class in classpath?

It doesn't. It's an annotation, and Java can run classes containing annotations that are not available in the classpath (by design).
This is what allows sharing classes between client and server, for example, that are annotated because the server needs the annotations, but which are useless at client-side.

Related

I created a maven project and added a dependency for web3j but when I create a new class and start importing libraries from Web3j it can't be found

This is the POM file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>MavenBackEndStructure</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.8.4</version>
</dependency>
</dependencies>
</project>
And this is my main class:
package com.mycompany.mavenbackendstructure;
import org.web3j.protocol.Web3j; //The Error is here
import org.web3j.protocol.http.HttpService; //And Here
public class Main {
public static void main(String[] args){
final Web3j client = new Web3j.build(
new HttpService(
url: "https://mainnet.infura.io/v3/435e21e85fd0498ea8fee6c77e0cc48f"
)
);
}
}
I tried to search the dependency at the Maven repository but still couldn't find it, So I don't know what's the problem that making the main can't see these libraries if someone could help me.
Thanks in advance.

#Controller , #RestController and #Component not working in child package in Spring boot multi module maven project

Parent POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sit</groupId>
<artifactId>multi-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>one</module>
<module>app</module>
</modules>
<packaging>pom</packaging>
<name>multi-module</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
app module POM
spring boot main class resides on this module
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>multi-module</artifactId>
<groupId>com.sit</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>app</artifactId>
<dependencies>
</dependencies>
</project>
One module (child module) POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>multi-module</artifactId>
<groupId>com.sit</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>one</artifactId>
<packaging>jar</packaging>
<dependencies>
</dependencies>
</project>
Spring Boot Main Class from app module
package com.sit.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan(basePackages = {"com.sit"})
#EntityScan(basePackages = {"com.sit"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
AppController from app module
package com.sit.app.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
#Controller
public class AppController {
#GetMapping(value = "/app")
public String getPage(){
return "app";
}
}
OneController class from one module
package com.sit.one.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
#Controller
public class OneController {
#GetMapping(value = "/one")
public String getPage(){
return "one";
}
}
When I run the project AppController.java is working fine by "/app" url.But when I try to access "/one" url of OneController.java ,I got the error page.No #Controller or #RestController is working from child (one) module.To solve this issue I added #ComponentScan(basePackages = {"com.sit"}) in Application.java, but still I am getting the error page.Can any help please.Thanks in advance.
The issue is that com.sit:one is not on the classpath of com.sit:app. Due to this, none of the classes of the one module can be found when you start the application module.
The solution is to make sure that the com.sit:one module is a dependency of com.sit:app by adding the following to pom.xml of the application module:
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>one</artifactId>
<version>${project.version}</version>
</dependency>
However, this is not enough, since the spring-boot-maven-plugin will create a fat JAR of both modules, while you only need a fat JAR of your application module (the runnable module).
I suggest that you move the <plugin> to the pom.xml of the application module:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
And then you should remove the plugin from the parent pom.xml.

How to develop a Maven Multi-module application using the Spring Boot framework

I have a maven multi-module Java project that runs perfectly, until I try to include the Spring Boot framework. The main module, called core, gets called OK, but all the rest are unable to implement the interface I declared in Core.java.
This error gets thrown:
[ERROR] /F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[12,30] cannot find symbol
[ERROR] symbol: class Service
[ERROR] /F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[13,5] method does not override or implement a method from a supertype
The project structure:
mvnmodularapp/
/pom
mvnmodularapp/core/
/src
/target/core-1.0-SNAPSHOT.jar
mvnmodularapp/module1
/src
/target/module1-1.0-SNAPSHOT.jar
THe file/directory structure:
core/src/java/service/
Service.java
core/src/java/service/impl
CoreServiceImpl.java
module1/src/java/service/impl
ModuleServiceImpl.java
Service.java
public interface Service {
String getName();
}
CoreServiceImpl.java in core
#Service
public class ServiceImpl implements Service {
#Override
public String getName() {
return "Hi. You called me from ServiceImpl in Core";
}
}
ModuleServiceImpl.java in module1
#Service
public class ModuleServiceImpl implements Service {
#Override
public String getName() {
return "Hi. You called me from ModuleServiceImpl in module1, but I can\'t pick up right now. Sorry";
}
}
THE POMS
mvnmodularapp pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mvnmodularapp</groupId>
<artifactId>mvnmodularapp</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>core</module>
<module>module1</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
core pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mvnmodularapp</artifactId>
<groupId>com.mvnmodularapp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Core</name>
<description>SudenGut application</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<start-class>main</start-class>
<java.version>1.8</java.version>
<tomcat.version>8.0.24</tomcat.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--- this will not be enough to provide a cool app :) -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
module1 pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mvnmodularapp</artifactId>
<groupId>com.mvnmodularapp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module1</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<start-class>baseview.impl.BaseViewImpl.ModuleServiceImpl</start-class>
<java.version>1.8</java.version>
<tomcat.version>8.0.24</tomcat.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--- this will not be enough to provide a cool app :) -->
<dependency>
<groupId>com.mvnmodularapp</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Dependencies between modules are correctly declared but you have one thing that is not configured as required.
I am not sure it will solve your problem but it could.
You declare in each module :
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
It should be required only for the module which bootstraps the Spring Boot application.
If you want have multiple web applications in the same Spring Boot container, you could read this question.
As you say that it works before adding Spring Boot I think that the problem should be solved with previous recommandation.
But if it is not enough, below are other guessworks which could be the cause of the compilation error. I rely on this error message :
[ERROR]
/F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[12,30]
cannot find symbol [ERROR] symbol: class Service
The Service class is not imported in ModuleServiceImpl.
If I rely on what you write in your question, you should add :
import service.Service in the imports of ModuleServiceImpl.
The core module doesn't create a JAR with the service.Service class.
So, importing it doesn't solve the problem of the module1 module.
You should perform a mvn clean install from the core module and check that the jar contains the compiled class : service.Service in the good package.

SpringBoot doesn't recognize RestController from another module in multi module Java application

I've spent quite some time but I can't get past this (configuration) issue.
Technology stack : Java (1.8), Springboot (starter-parent, starter-web), Maven, IntelliJ IDEA
Description: Trying to create a multi module Java application composed (at first) of 2 modules :
core module : main module (main business logic, every other module should see and interact through this one). This module contains the main application class.
webgateway module : Simple Rest Controller that will map a request and make calls to the core module
Issue: Springboot doesn't load/scan the RestController from the webgateway module => 404 error when sending the http request
Github repo : https://github.com/Sorin-J/Greeter
Project configuration :
Greeter
|
+ pom.xml (parent pom)
|
+ -- core
| |
| + ...
| |
| + pom.xml
|
+ -- webgateway
|
+ ...
|
+ pom.xml (depends on core pom.xml)
Parent pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bet.jbs</groupId>
<artifactId>Greeter</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>core</module>
<module>webgateway</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
core module pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Greeter</artifactId>
<groupId>com.bet.jbs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>core</artifactId>
</project>
webgateway module pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Greeter</artifactId>
<groupId>com.bet.jbs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>webgateway</artifactId>
<dependencies>
<dependency>
<groupId>com.bet.jbs</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
MainApplication class from core module :
package com.bet.jbs.core;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScan(basePackages = {"com.bet.jbs.core", "com.bet.jbs.webgateway"})
#EnableAutoConfiguration
public class MainApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(MainApplication.class, args);
}
}
GreetingController class from webgateway module :
package com.bet.jbs.webgateway.controller;
import com.bet.jbs.core.util.GreetingGenerator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class GreetingController {
#RequestMapping(value = "/webgreeting", method = RequestMethod.GET)
public String getGreeting() {
return "WEBGATEWAY module says " + GreetingGenerator.getRandomGreeting();
}
}
Just to test that an identical REST controller would work fine if it is located in the core module, I've created a similar GreetingController class also in core module (and this one works fine) :
package com.bet.jbs.core.controller;
import com.bet.jbs.core.util.GreetingGenerator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/*
* This REST controller should not be in the CORE component.
* It is just for proving that this controller is recognized and the other one from WEBGATEWAY component is not.
*
*/
#RestController
public class GreetingController {
#RequestMapping(value = "/coregreeting", method = RequestMethod.GET)
public String getGreeting() {
return "CORE module says " + GreetingGenerator.getRandomGreeting();
}
}
The Spring Boot main application is in the core module, which does not have a dependency on the webgateway module. Therefore the class with the controller will not be present at runtime and can not be discovered by spring.
Fix: Add dependency to the webgateway to the core or move the launcher/main class to the webgateway module.
You can also use a third module that does the launching and has the dependencies to core and webgateway.
I was stuck in this problems since last one day... The solution below will save you a lot of time for sure!
Here is how I solved it:
Create a separate module for main Application class.
x-service
pom.xml (parent)
child1-module
pom.xml
child2-module
pom.xml
app-module
pom.xml (contains dependency for both child1 and child2)
src/main/java/Application.java
Add all the modules as dependencies in the pom of the module having application class.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>x-service</artifactId>
<groupId>com.a.b.c</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>app-module</artifactId>
<dependencies>
<dependency>
<groupId>com.a.b.cs</groupId>
<artifactId>child1-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.a.b.cs</groupId>
<artifactId>child1-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
Keep the main pom as parent pom.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>child1-module</module>
<module>child2-module</module>
<module>app-module</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.a.b.c</groupId>
<artifactId>x-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
Add the parent pom artifect as parent in all child poms.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dxg-service</artifactId>
<groupId>com.a.b.c</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.a.b.c</groupId>
<artifactId>child1-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.a.b.c</groupId>
<artifactId>child2-modulde</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
More details here:
https://medium.com/macoclock/how-create-multi-module-project-in-intellij-on-macos-50f07e52b7f9
I had added the correct dependency and Spring Boot still did not find my new Controller. Seems like IntelliJ IDEA (2020.3.2) or Maven (3.3.6) don't update properly when a module gets added. This solved it for me:
Maven:
clean + install on the parent project
In IntelliJ from the file menu Invalidate Caches / Restart..
as mentioned you should have parent pom with setup spring web starter in that and other modules must get that parent pom as their parent. be carful to add your root pom project to maven. to do this open maven in IDE and add pom.

spring-boot application fails to start

I recently start to develop a web application with spring-boot, anf, following the guide in the offical site, manage to create this two files:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>app</name>
<url>http://maven.apache.org</url>
<properties>
<start-class>com.spring.app.Application</start-class>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Application.java
#Configuration
#EnableAutoConfiguration
#ComponentScan
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
}
but when I try run the application with java -jar appname i get the error: Cannot find the main class: com.spring.app.Application. Program will exit, and in the terminal: Exception in thread "main" java.lang.NoClassDefFoundError: com/spring/app/Application.
What I am doing wrong?
You have 2 things to do in your pom.xml.
First change the start-class to your application class.
Second add the super-cool Spring Boot maven builder to your pom.
Something like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>beanlist</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<start-class>com.sample.Application</start-class>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project
Then use "mvn install" to create your jar. Your code runs just fine.
Value of the following tag should be changed to match real class with main method. Try to specify correct package and run "mvn install"
<start-class>com.sample.Application</start-class>
you Application is in this package:
package com.spring.app;
which means your Application full path should be package com.spring.app.Application and your error code said com.spring.Application not found.
Maybe you should check your run configuration
I was also getting same error. Its got resolved by converting the project to maven in eclipse as mentioned in below image:
Hope it will you. :)

Categories