Меню

An illegal reflective access operation has occurred spring ошибка

My JDK 9+181 Spring Boot 2.0.0.BUILD-SNAPSHOT CLI application displays this warning on startup:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (jar:file:/home/jan/src/fm-cli/target/fm-cli-0.1.0-SNAPSHOT.jar!/BOOT-INF/lib/spring-core-5.0.0.RELEASE.jar!/) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1

This is a console application, so I need to disable this warning — how can I do that?

Note: This question asks the specific question of how to disable this warning triggered by Spring; it is not a duplicate of JDK9: An illegal reflective access operation has occurred. org.python.core.PySystemState which deals with a similar symptom in a different library.

asked Oct 10, 2017 at 16:21

Jan Nielsen's user avatar

Jan NielsenJan Nielsen

10.5k13 gold badges65 silver badges115 bronze badges

0

In JDK 9+, add the following option to the JVM to disable the warning from Spring’s use of CGLIB:

--add-opens java.base/java.lang=ALL-UNNAMED

for example:

java --add-opens java.base/java.lang=ALL-UNNAMED -jar target/*.jar

No need to report it; it’s a known Spring bug.

This happens because the new JDK 9 module system detected an illegal access that will be disallowed sometime in the (near) future. You can read more about the JDK 9 Module system here.

Update:

A fix for this issue is available JDK 9+ with Spring 5.1+.

answered Oct 10, 2017 at 16:21

Jan Nielsen's user avatar

Jan NielsenJan Nielsen

10.5k13 gold badges65 silver badges115 bronze badges

16

This also happened to me on JDK 11 and Spring Boot 2.2.1 (Spring Core 5.2.1).

What helped was removing the dependency to org.springframework.boot:spring-boot-devtools, as suggested in some comments to Spring Framework issue #22814.

answered Feb 19, 2020 at 11:09

Cos64's user avatar

Cos64Cos64

1,5781 gold badge18 silver badges30 bronze badges

Adding to Jan Nielsen answer above, if you are using Intellij and Spring Boot 2.0.3, which depends on Spring core 5.0.7, you are still stuck and do not have the fix.

The way out for me needed two things:

  • Add the —add-opens mentioned by Jan to your run/debug configuration. Just edit the configuration and look under Environment / VM Options. This takes care of silencing some of the «illegal access messages».

  • I also needed to add a dependency to the jaxb-api library. I got the hint from ValentinBossi comment on this spring github issue.

answered Jun 16, 2018 at 0:41

luv2learn's user avatar

luv2learnluv2learn

5565 silver badges12 bronze badges

When using the spring initializer, make sure you use the latest version of Spring Boot. It automatically gets Spring core 5.1 or greater for you and you won’t see the error when you run the project.

So you don’t have to worry about editing any JVM configuration.

answered Aug 13, 2018 at 20:29

Onome Sotu's user avatar

Onome SotuOnome Sotu

6469 silver badges16 bronze badges

1

After these warnings, if your app is still not working then add this dependency to your pom.xml

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

This helped me!!

answered Sep 6, 2018 at 12:45

kalariya parikshith's user avatar

I’m sitting on these versions and I still see the WARNING:

  • SpringBoot 2.4.1
  • Spring Core FW: 5.3.2 (my parent pom is Spring Boot’s, so this version is introduced by it, not manually set by me)
  • Java openjdk version «11.0.9.1» 2020-11-04

Removing the dependency from spring-boot-devtools also worked for me and the warning is no longer there at startup.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <optional>true</optional>
</dependency>

… At the end of the day I was just using the automatic re-start from my IDE upon any change in the classpath and I can leave without it.
The thing, though, is that theroreticatlly we should not worry about it, since according to Spring documentation:

Developer tools are automatically disabled when running a fully packaged application. If your application is launched using java -jar or if it’s started using a special classloader, then it is considered a “production application”

So one could expect that when the application is launched from command line the warning should go away (I haven’t verified that).

answered Dec 28, 2020 at 13:17

WinterBoot's user avatar

WinterBootWinterBoot

3421 gold badge3 silver badges13 bronze badges

1

Comments

@paulvi

@paulvi
paulvi

changed the title
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/Users/…/spring-core/4.3.22.RELEASE/spring-core-4.3.22.RELEASE.jar)

on JDK 8 — WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/Users/…/spring-core/4.3.22.RELEASE/spring-core-4.3.22.RELEASE.jar)

Mar 26, 2019

@paulvi
paulvi

changed the title
on JDK 8 — WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/Users/…/spring-core/4.3.22.RELEASE/spring-core-4.3.22.RELEASE.jar)

on JDK 11 — WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/Users/…/spring-core/4.3.22.RELEASE/spring-core-4.3.22.RELEASE.jar)

Mar 27, 2019

In Java programming, the reflection API lets you examine the internal properties of a running Java program and manipulate them. The name of all its members can be displayed by a Java class, for example.

The «an illegal reflective access operation has occurred» warning message is related to the unauthorized access to parts of the JDK made by tools and libraries using reflection API.

In this article, we are going to show you what «an illegal reflective access operation has occurred» means and how to fix it.

From JDK9, an implementation may provide static access, i.e. by compiled bytecode or may provide a way to invoke its run-time system with one or more packages of one or more of its modules open to code in all unnamed modules, i.e. to code on the classpath.

If the run-time system is invoked in this way, and if by doing so some invocations of the reflection APIs succeed where otherwise they would have failed.

In such cases, you’ve actually ended up making a reflective access which is «illegal» since in a pure modular world you were not meant to do such accesses.

In future releases of the JDK, this illegal access to reflective data will eventually be disabled. JDK 9, 10 and 11 permits it by default. Here is the warning message:

WARNING: An illegal reflective access operation has occurred

WARNING: Illegal reflective access by com.tangosol.internal.util.ObjectFormatter (file:/opt/javaclasses/coherence/coherence.jar) to field java.lang.ref.Reference.referent

WARNING: Please consider reporting this to the maintainers of com.tangosol.internal.util.ObjectFormatter

WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations

WARNING: All illegal access operations will be denied in a future release

Permit illegal reflective access

Recent JDK versions still, by default, permits illegal access. Alternatively, in future releases, you can pass --illegal-access=permit to the script and it would opens every package in every explicit module to code in all unnamed modules, i.e., code on the class path, just as --permit-illegal-access does today.

The first illegal reflective-access operation causes a warning to be issued, as with --permit-illegal-access, but no warnings are issued after that point. This single warning will describe how to enable further warnings.

Avoid illegal reflective access

The «an illegal reflective access operation has occurred» is merely a warning message and only indicating there is reflective access to JDK internal class without doing anything about it. In the meantime, you can either

  • You can specify the reflective access explicitly using java’s –add-opens parameter. The migration guide for JDK 11 provides more information.
  • Use a compatible and certified version of your software. If the software is actively maintained, then the developers are now aware of the warning and a fix may come in the near future.

Делал telegram bota’a по документации (https://github.com/rubenlagus/TelegramBots/wiki/Ge…)
Выскакивают ошибки:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/C:/Users/Vladp/.m2/repository/com/google/inject/guice/4.2.2/guice-4.2.2.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use —illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

Вот код java и xml:

import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

public class Bot extends TelegramLongPollingBot {
    public static void main (String[] args) {
        ApiContextInitializer.init();
        TelegramBotsApi botsApi = new TelegramBotsApi();
        try {
            botsApi.registerBot(new Bot());
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }

    }
    @Override
    public void onUpdateReceived(Update update) {
        if (update.hasMessage() && update.getMessage().hasText()) {
            SendMessage message = new SendMessage() // Create a SendMessage object with mandatory fields
                    .setChatId(update.getMessage().getChatId())
                    .setText(update.getMessage().getText());
            try {
                execute(message); // Call method to send the message
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public String getBotUsername() {
        return "BotTotCampot_bot";
    }

    @Override
    public String getBotToken() {
        return "Тут мой токен";
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>telebot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>7</source>
                    <target>7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.telegram</groupId>
            <artifactId>telegrambots</artifactId>
            <version>4.9.1</version>
        </dependency>


        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.13</version>
        </dependency>
    </dependencies>

</project>

Illegal reflection warning does not seem to affect anything

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.xnio.nio.NioXnio$2 (file:/Users/jixianzhilu/.m2/repository/org/jboss/xnio/xnio-nio/3.3.8.Final/xnio-nio-3.3.8.Final.jar) to constructor sun.nio.ch.KQueueSelectorProvider()
WARNING: Please consider reporting this to the maintainers of org.xnio.nio.NioXnio$2
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

Ignore this warning for jdk11

	import java.lang.reflect.Field;
	import java.lang.reflect.Method;
	/**
	 * Ignore illegal reflection warnings for jdk11
	 */
    @SuppressWarnings("unchecked")
    public static void disableAccessWarnings() {
        try {
            Class unsafeClass = Class.forName("sun.misc.Unsafe");
            Field field = unsafeClass.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            Object unsafe = field.get(null);

            Method putObjectVolatile = unsafeClass.getDeclaredMethod("putObjectVolatile", Object.class, long.class, Object.class);
            Method staticFieldOffset = unsafeClass.getDeclaredMethod("staticFieldOffset", Field.class);

            Class loggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
            Field loggerField = loggerClass.getDeclaredField("logger");
            Long offset = (Long) staticFieldOffset.invoke(unsafe, loggerField);
            putObjectVolatile.invoke(unsafe, loggerClass, offset, null);
        } catch (Exception ignored) {
        }
    }

For example, enable in springboot

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

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • An illegal memory access was encountered ошибка
  • An existing connection was forcibly closed by the remote host ошибка