警告:发生了非法反射访问操作(Java中的可移植OpenCV)。

6
我希望制作一个依赖于maven文件pom.xml的可移植的opencv应用程序。
简化代码如下:
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());
    }
}

我将这个添加到pom.xml文件中:

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

它适用于以下警告(针对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]

更新:并且针对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'.

在Maven依赖文件夹中折叠opencv jar文件可以看到所有库都是灰色的,如下图所示:

maven dependency folder for opencv 3.2

如果我忽略了这个警告并未使用警告信息中提到的命令链接库,会发生什么? 因为使用Java让opencv更易携带很简单,我想知道是否有问题,以便在将来继续使用此方法。 我正在使用JDK 9、Eclipse Oxygen.2和Fedora 27。 将来,应用程序的目标可能是Windows。

看起来它正在黑入java.lang.ClassLoader中的私有字段。也许这个库的作者们不知道System.load(在其中指定文件路径而不是名称加载库)。 - Alan Bateman
@AlanBateman 有什么我可以做来修复它吗?我在 GitHub 上发表了评论,并要求作者查看问题。 - Amiri
抱歉,我不熟悉OpenCV,但将问题提交给维护者的问题跟踪器似乎是解决此问题的正确方法。 - Alan Bateman
2个回答

5
这是因为Java 9新增了非法访问的检查,所以在Java 9发布后经常出现此类安全警告。要解决这个问题,最好的方法是向维护者报告错误并等待补丁更新发布。
然而,您可以自行决定是否启用安全功能(例如堆栈保护),但需自担风险,并取决于您在讨论的程序包的使用和范围。

1
当我将一个项目从Java 8升级到Java 11时,对于org.codehaus.groovy:groovy-eclipse-batch插件,我遇到了这个问题。将其从版本2.4.16-02升级到3.0.1-01(截至目前的最新版本)解决了问题,并在构建过程中消除了警告消息。因此,询问或寻找新版本绝对是解决此问题的方法。 - Olivier

3
自Java更新到9版本以后,会出现“非法反射访问操作已发生”警告。
当我从jdk1.8升级到jdk 9+(jdk11有更多支持)时,在多个项目中通过修改pom.xml文件来解决了Maven Build和Maven Install的问题。以下是两个示例,可以消除“非法反射访问操作已发生”的构建消息:
将版本从:
    <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>

同时还将artifactId和版本号从以下内容进行了更改:

<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>

致:

<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>

当我重新运行Maven Build(clean compile package)或Maven Install时,"illegal reflective access operation has occurred"就消失了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接