简体   繁体   中英

IllegalAccessError exception when executing a simple Spring Boot application

I'm following the official spring boot guide:

http://projects.spring.io/spring-boot

My Machine Info:

Apache Maven 3.1.0 
Maven home: /Users/abshammeri/apps/apache-maven-3.1.0
Java version: 1.7.0_11, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.5", arch: "x86_64", family: "mac"

POM.xml

<groupId>org.test</groupId>
<artifactId>example</artifactId>
<version>0.1.0</version>


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.6.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<properties>
    <start-class>hello.Application</start-class>
</properties>

<build>
    <plugins>
        <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>

        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

And the Code:

package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

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

but I get the following Exception every time I try to mvn package && java -jar example.jar , using apache maven 3 and java 1.7 ,

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.IllegalAccessError: tried to access method org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(Ljava/lang/Class;Ljava/lang/ClassLoader;)Ljava/util/List; from class org.springframework.boot.SpringApplication
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:367)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:358)
    at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:228)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:204)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    at hello.Application.main(Application.java:17)

Thanks,

I'll answer my question based on the helpful note that's mentioned by @AndyWilkinson in the question comments, There were old spring jars (3.2) in /Library/Java/Extensions/ (it's MAC OSX path ) , I've removed them. Now java loads apache Maven jars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM