16

I obtained an exception when generated a stub for a WS service by wsimport in /target/class/..... and run a spring boot application with devtools.

Caused by: java.lang.IllegalArgumentException: com....Service referenced from a method is not visible from class loader

I found that an issue with spring devtools class loader, RestartClassLoader, because of two different references to a Class (RestartClassLoader and AppClassLoader)

private static void ensureVisible(ClassLoader ld, Class<?> c) {
    Class<?> type = null;
    try {
        type = Class.forName(c.getName(), false, ld);
    } catch (ClassNotFoundException e) {
        if (type != c) {
            throw new IllegalArgumentException(c.getName() +
                    " referenced from a method is not visible from class loader");
        }
    }
}

I was trying to add a reference to a jar file in spring-devtools.properties to restart.include=/.....jar

Spring Boot 2.0.0.RELEASE Java 9

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111

2 Answers2

15

Since they are generated classes you have to exclude them from Spring Devtools "restart" classloader.

  1. Create a /src/main/resources/META-INF/spring-devtools.properties file
  2. Add properties like restart.exclude.* to exclude classes from restart classloader (example, you can use restart.exclude.mygeneratedclasses=/*[generated]*.class to exclude all classes with generated word as part of package or class name)

  3. Done. Now you can use devtools and have no issues with WS generated classes.

Reference:

[1] https://github.com/spring-projects/spring-boot/issues/4529

[2] https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-customizing-classload

Fernando Cappi
  • 306
  • 2
  • 7
  • Alternatively, you can try to include JAR (which uses classloader out of spring-devtools) into spring-devtools. See: https://stackoverflow.com/a/61088774/6451286 – pufface Apr 08 '20 at 08:05
1

Or simply remove a Spring Dev Tools dependency if this is won't affect your application:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <scope>runtime</scope>
</dependency>
Zon
  • 18,610
  • 7
  • 91
  • 99