I'm pretty new to Maven and Java itself.

I'm trying to run my project's .jar file that is produced after running :

  1. maven install clean

  2. maven package

When I run the jar file, I get the following error :

enter image description here

I have tried several solutions online but still cannot make sense of it or how to resolve it. I can edit this question and share my pom.xml file if necessary. this is the dependency version I am using :

  <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>6.0.0</version>
      <scope>provided</scope>
  </dependency>
1

There are 1 best solutions below

5
Radu Sebastian LAZIN On

Remove the @Autowired from the httpRequest variable in the ApiController, you shouldn't have a bean of type HttpServletRequest, that's an object that gets created for each request. You can add a HttpServletRequest parameter to any method in your controller class that is annotated to be a HTTP method (@GetMapping) and Spring will automatically initialize the parameter with a reference to the current HttpServletRequest.

Scope provided means that the dependency should be provided at runtime by JDK or a container. You have 2 options:

Option 1. Remove the scope:

  <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>6.0.0</version>
  </dependency>

Option 2. Add the dependency to the classpath:

java -classpath <path_to_jakarta_api> -jar <your_jar>

just be careful that the second option overrides the entire classpath so if you have some other jars in your current folder or elsewhere seperate them with ;.

Example: -classpath .;lib;<path_to_jakarta> this will search in current folder, lib subfolder, and path

Option 3: If you have a web application and use Tomcat keep the <scope>provided</scope> since those will be included in the classpath automatically by Tomcat.