1
votes

I tried to use Eclipse to run the Spring Framework "Quick Start" tutorial at http://projects.spring.io/spring-framework/

I firstly built a Maven Project in Eclipse, and added three .java files. The codes are as follows

package hello;

public interface MessageService {
    String getMessage();
}

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessagePrinter {

    final private MessageService service;

    @Autowired
    public MessagePrinter(MessageService service){
        this.service = service;
    }

    public void printMessage(){
        System.out.println(this.service.getMessage());
    }

}

package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

@Configuration
@ComponentScan
public class Application {

    @Bean
    MessageService mockMessageService(){
        return new MessageService(){
            public String getMessage() {
                return "Hello World!";
            }};
    }

    public static void main(String args[]){
        ApplicationContext context = 
                new AnnotationConfigApplicationContext(Application.class);
        MessagePrinter printer = context.getBean(MessagePrinter.class);
        printer.printMessage();
    }

}

Then I right clicked the Eclipse and choose "run as > Java Application". Two error messages jumped out:

  1. Error: A JNI error has occured, please check your installation and try again

  2. A Java Exception has occurred.

and the console showed

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/beans/factory/ListableBeanFactory at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:763) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) at java.net.URLClassLoader.access$100(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:368) at java.net.URLClassLoader$1.run(URLClassLoader.java:362) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:361) 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) 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.springframework.beans.factory.ListableBeanFactory 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) ... 19 more

I have tried to add more Spring dependencies (including aop aspects beans context context-support core ...) in the POM file, but that didn't work.

1
try adding the spring-beans dependency to your pom - J J
I have tried to add Spring dependencies (including aop aspects beans context context-support core ...) in the POM file, but that didn't work. - Jenning Lang
Since you are using Eclipse to run the app, you would need to add the corresponding jar file to the project build path. Right click on the project -> Build Path -> Configure Build Path -> Libraries -> Add external Jar - Balaji Katika

1 Answers

2
votes

I have already fixed this problem. In fact this problem had little relationship to "A JNI error has occured".

The cause of the this problem is that an error(s) occured when Eclipse download Maven dependency jar file, namely "spring-beans".

So I (manually) deleted all the files in "....m2\repository\org\springframework\spring-beans", and used Eclipse to rebuild the project. In this way the problem was fixed.