WARNING: An illegal reflective access operation has occurred (portable opencv in java)

21,322

Solution 1

This occurred because of Java 9 new checks of illegal access, and it is common to have such kind of security warnings after the release of java 9. The permanent solution to this is to report the bug to the maintainers and wait for them to release a patch update.

However only at your own risk, yes you can continue without security features i.e stack guard, depending on your use and scope of this package under discussion.

Solution 2

Since the Java update 9, the "illegal reflective access operation has occurred" warning occurs.

I have resolved this from Maven Build and Maven Install by modifying my pom.xml file in multiple projects when I did upgrade to from jdk1.8 to jdk 9+ (jdk11 has more support). Following are 2 examples that get rid of the "illegal reflective access operation has occurred" build message:

Change version from:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <warSourceDirectory>WebContent</warSourceDirectory>
            <webXml>WebContent\WEB-INF\web.xml</webXml>
        </configuration>
    </plugin>

To:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.1</version>
        <configuration>
            <warSourceDirectory>WebContent</warSourceDirectory>
            <webXml>WebContent\WEB-INF\web.xml</webXml>
        </configuration>
    </plugin>

And also changed the artifactId and version from:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

To:

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
    
    ..
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.1</version>
    </plugin>
    ..
</build>

When I re-run Maven Build (clean compile package) or Maven Install, the "illegal reflective access operation has occurred" is gone.

Share:
21,322
Amiri
Author by

Amiri

Life is as long as you can achieve whatever you want, and, is as short as you cannot conceive what really you want.

Updated on May 25, 2021

Comments

  • Amiri
    Amiri almost 3 years

    I want to make a portable opencv application which the dependency is added to maven file pom.xml.

    Simplified code is :

    import org.opencv.core.Mat;
    
    public class Builder {
    
        public static void main(String[] args) {
    
            nu.pattern.OpenCV.loadShared();
            System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
    
            Mat mat = new Mat(4,3,1);
            System.out.println(mat.dump());
        }
    }
    

    I added this to pom.xml:

    <dependency>
          <groupId>org.openpnp</groupId>
          <artifactId>opencv</artifactId>
          <version>3.2.0-0</version>
          <scope>compile</scope>
    </dependency>
    

    It works with the following warning for java 9:

    WARNING: An illegal reflective access operation has occurred
    WARNING: Illegal reflective access by nu.pattern.OpenCV$SharedLoader (file:/home/martin/.m2/repository/org/openpnp/opencv/3.2.0-0/opencv-3.2.0-0.jar) to field java.lang.ClassLoader.usr_paths
    WARNING: Please consider reporting this to the maintainers of nu.pattern.OpenCV$SharedLoader
    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 HotSpot(TM) 64-Bit Server VM warning: You have loaded library /tmp/opencv_openpnp6598848942071423284/nu/pattern/opencv/linux/x86_64/libopencv_java320.so which might have disabled stack guard. The VM will try to fix the stack guard now.
    It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.
    101, 115,  47;
     108, 105,  98;
      47, 108, 105;
      98, 111, 112]
    

    UPDATE: And the following warning for java 8:

    Java HotSpot(TM) 64-Bit Server VM warning: You have loaded library/tmp/opencv_openpnp3835511967220002519/nu/pattern/opencv/linux/x86_64/libopencv_java320.so which might have disabled stack guard. The VM will try to fix the stack guard now.
    It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.
    

    Collapsing the opencv jar in maven dependency folder shows that all the libraries are in grayed color as depicted here:

    maven dependency folder for opencv 3.2

    What if I ignore this warning and don't link the libraries with the mentioned command in the warning message? Because it was very simple to make the opencv portable with java and I want to realize if there is no problem then continue this method in future.

    I use JDK 9, Eclipse Oxygen.2, Fedora 27.

    In future, the application's target might be windows.

  • Olivier
    Olivier about 4 years
    This happened to me when upgrading a project from Java 8 to Java 11, for the org.codehaus.groovy:groovy-eclipse-batch plug-in. Upgrading it from version 2.4.16-02 to 3.0.1-01 (latest version to date) did the trick and made the warning message disappear during the build. So, asking or looking for a new version is definitely a solution to this problem.