1
votes

I'm trying to run my spring-boot project using the command 'java -jar XXXX.jar' but getting the following error:

Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: org/modelmapper/ModelMapper at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.privateGetMethodRecursive(Class.java:3048) at java.lang.Class.getMethod0(Class.java:3018) at java.lang.Class.getMethod(Class.java:1784) at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526) Caused by: java.lang.ClassNotFoundException: org.modelmapper.ModelMapper at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 7 more

I'm using modelmapper and I declare it as a bean in the following way:

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
public class StartApplication {

    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }

    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
}

In the pom I define the following addiction:

<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>2.0.0</version>
</dependency>

what could be the problem?

1
Did you try to rebuild clean and build? - Jonathan JOhx
Yes, I tried, but I always have the same problem. - Maurizio Rizzo
Ok, did you get the same message error when it runs? - Jonathan JOhx
Yes, I always have the same error - Maurizio Rizzo
Sorry, did you get the same on IDE? looks like your JDK is not answering ... - Jonathan JOhx

1 Answers

0
votes

I fixed this issue by doing the following steps:

Step1: Add ModelMapper Library dependency;

<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>2.4.3</version>
</dependency>

Step2: define the ModelMapper bean in our Spring configuration:

import org.modelmapper.ModelMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringBootAppDemo {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(SpringBootAppDemo.class);
    }
    
    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }
}

Step3: Inject and Use ModelMapper

Entity Class

@Entity
public class Student {
    private Long id;
    private String firstName;
    private String lastName;
    private int year;
}

DTO Class

public class StudentDto {
    private String firstName;
    private String lastName;
    private int year;
}

Example

import org.modelmapper.ModelMapper;

@Autowired
private ModelMapper modelMapper;

Student student = new Student(...);
ModelMapper modelMapper = new ModelMapper();
StudentDto studentDto = modelMapper.map(student, StudentDto.class);

Step4: Stop your project;

Step5: Run the command line mvn clean;

Step6: Run your project again;

I hope this will help!!