Issue
Below I describe more about the issue I have.
Description I making a program using Spring REST Interface (see code below) and I have problem to compile code with Maven (see problem section). I have no problem to compile the code using my IDE Eclipse.
Problem When I compile my code with Maven in command line in Windows I use mvn clean compile but then get the fault message: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project stocks-rest: Compilation failure: Compilation failure: [ERROR] /C:/tobbe/eclipse/student-services/src/main/java/Rest/StocksRestApi.java:[25,18] package Exceptions does not exist [ERROR] /C:/tobbe/eclipse/student-services/src/main/java/Rest/StocksRestApi.java:[83,184] cannot find symbol [ERROR] symbol: class ElementSizeException [ERROR] location: class Rest.StocksRestApi
I have the jar in local reposity (m2) and it contain ElementSizeException.
File structure where ElementSizeException is stored (other project): C:\tobbe\eclipse\ws_privat\Other\src\main\java\Exceptions (Enums)\ElementSizeException.java
My code:
/**
* Version 1.0.2
*/
package Rest;
import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.lang.instrument.IllegalClassFormatException;
import java.net.MalformedURLException;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import Enums.Market;
import Enums.REA_Stock_State;
import Enums.Stock_State;
**Fault**import Exceptions.ElementSizeException;
import REA.REAMain;
import REA.REAStock;
import Stocks.Logger;
@SpringBootApplication
@RestController
public class StocksRestApi
{
private Logger iLogger = null;
public StocksRestApi()
{
iLogger = new Logger();
}
public static void main(String[] args)
{
SpringApplication.run(StocksRestApi.class, args);
}
@GetMapping(value="/rea")
public String rea() throws HeadlessException, MalformedURLException, IOException, AWTException, InterruptedException, UnsupportedFlavorException, IllegalClassFormatException, **Fault**ElementSizeException
{
List<REAStock> stocks = REAMain.doAnalyze(true, Market.LARGE_CAP_STHLM_KEYFIGURE, REA_Stock_State.REA_ANALYZE, Stock_State.UNKNOWN, iLogger);
String allStocks = "";
for(int index = 0; index < stocks.size(); index++)
{
allStocks += index + 1 + ". " + stocks.get(index).getName() + " ";
if(index < 20)
{
allStocks += "<br>";
}
if(index == 19)
{
allStocks += "------------------------------<br>";
}
}
return allStocks;
}
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>tobbe</groupId>
<artifactId>stocks-rest</artifactId>
<packaging>jar</packaging>
<version>1.0.1-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>tobbe</groupId>
<artifactId>Stocks_Backend</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
</project>
Question Why do I get compilation fault and why is not ElementSizeException (other own project which jar is in m2 and contain functionaly) found when using Maven?
Solution
Maven is unable to find the a dependency containing the imported class, and it is most likely because a dependency is missing or incorrectly referenced in the POM.
Verify that the dependency:
<dependency>
<groupId>tobbe</groupId>
<artifactId>Stocks_Backend</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
Is actually matching the GAV for the artifact installed by the project creating the required dependency, and that it is containing "Exceptions.ElementSizeException".
Also consider using the few simple Java naming conventions when naming your packages, classes, methods, and other Java types.
Answered By - johan
Answer Checked By - Mary Flores (JavaFixing Volunteer)