1

When I use the command mvn spring-boot:run the project compiles and starts perfectly. However, when I use the play-button in my IDE (IntelliJ), I get the following error:

Description:
Parameter 3 of constructor in com.example.module.services.PdfService required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found. 

What might be the reason for this? I`d love to use the debugging and dev tools provided by the IDE.

What I 've tried:

  • Select Build->Rebuild Project
  • Clicking File>Invalidate caches/ restart
  • mvn clean -> Build -> Make Project
  • Maven -> Reimport
  • Checked that there is no excludes in Preferences | Build, Execution, Deployment | Compiler | Excludes

My pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

The Service in which the error occurs

@Service
public class PdfService {

private final Logger logger = LoggerFactory.getLogger(PdfService.class);

private PdfCreator pdfCreator;

private ConfigProperties properties;

private ReceiptService receiptService;

private JavaMailSender javaMailSender;

private MessageSourceAccessor messageSourceAccessor;

@Autowired
public PdfService(PdfCreator pdfCreator,
                  ConfigProperties properties,
                  ReceiptService receiptService,
                  JavaMailSender javaMailSender,
                  MessageSourceAccessor messageSourceAccessor) {
    this.pdfCreator = pdfCreator;
    this.properties = properties;
    this.receiptService = receiptService;
    this.javaMailSender = javaMailSender;
    this.messageSourceAccessor = messageSourceAccessor;
}

Help is very much appreciated.

Greta
  • 300
  • 1
  • 10

2 Answers2

1

You need to add org.springframework.mail.javamail.JavaMailSender.jar as dependency as it is missing. You may follow the following steps to fix in intellij.

  1. Download the jar file.
  2. In IntelliJ Idea IDE, Go to File > Project Structure.
  3. Select Modules.
  4. In the dependecies section, select (+) icon > Select JARs or Directories
  5. Paste link to the location of the JAR file
  6. Select OK and Apply

Now, it will be fixed.

  • Like recommended here https://stackoverflow.com/questions/1367303/which-artifact-for-org-springframework-mail I added the Spring Context Support and Spring Support .jar-files to the dependencies. It´s still not working.. – Greta Nov 16 '20 at 21:22
  • Can you elaborate what you meant by 'still not working'? Mentioning the error will help anyone to find the cause much easier :) – ProgrammingEnthusiast Nov 17 '20 at 00:22
  • I recommend you to try out solution provided by Thangavel. Usually, adding modules resolves any error I get. I don't know more : – ProgrammingEnthusiast Nov 18 '20 at 15:14
0

You need to create Configuration class and tell them to container with @Bean I just tried. It works for me.

@Configuration
public class AppConfiguration {

    @Bean
    public JavaMailSender javaMailSender(){
        return new JavaMailSenderImpl();
    }

}

Please check my Gist on the below url,

https://gist.github.com/thangavel-projects/9c30e6fe141755cf471c9d574e7341b2

prostý člověk
  • 909
  • 11
  • 29
  • Hey @Thangavel Loganathan, thank you very much for your suggestion and sorry for my late answer. So if I add the JavaMailSender-Bean so my configuration class I get another error. Also I wonder why I would only need this piece of code when I start the application using IntelliJ. This is the new error: java.lang.NullPointerException: null at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:98) ~[na:na] at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) ~[na:na] at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92) ~[na:na] – Greta Nov 17 '20 at 17:57
  • Please read the https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8133521 ```"passing a null argument to a constructor or method of any class or interface in this package will cause a NullPointerException to be thrown."``` I think you are passing null inside ```Paths.get(null)``` like this. – prostý člověk Nov 17 '20 at 18:21
  • Moreover your issue resolved for your question raised here. so please you need to accept the answer for others reference in future. – prostý člověk Nov 17 '20 at 18:37