【问题标题】:WARNING: An illegal reflective access operation has occurred (portable opencv in java)警告:发生了非法反射访问操作(Java 中的便携式 opencv)
【发布时间】:2018-08-24 04:41:42
【问题描述】:

我想制作一个可移植的opencv 应用程序,将依赖项添加到maven 文件pom.xml

简化代码是:

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 显示所有库都是灰色的,如下所示:

如果我忽略此警告并且不将库与警告消息中提到的命令链接怎么办? 因为用java做opencv移植很简单,我想实现如果没有问题,以后继续这个方法。

我使用 JDK 9、Eclipse Oxygen.2、Fedora 27。

将来,应用程序的目标可能是 windows。

【问题讨论】:

  • 看起来它正在侵入 java.lang.ClassLoader 中的私有字段。可能是该库的作者不知道 System.load(您在其中指定库的文件路径而不是其名称)。
  • @AlanBateman 有什么办法可以解决吗?我在GitHub上评论,请作者看问题。
  • 抱歉,我不知道 opencv,但向维护者问题跟踪器提交问题似乎是解决此问题的正确方法。

标签: java maven opencv java-9 javacv


【解决方案1】:

这是因为 Java 9 对非法访问的新检查造成的,并且在 Java 9 发布后出现这种安全警告是很常见的。永久的解决方案是向维护人员报告错误并等待他们发布补丁更新。

但是,只有您自己承担风险,是的,您可以继续不使用安全功能,即堆栈保护,具体取决于您所讨论的此软件包的用途和范围。

【讨论】:

  • 这发生在我将项目从 Java 8 升级到 Java 11 时,用于 org.codehaus.groovy:groovy-eclipse-batch 插件。将其从版本 2.4.16-02 升级到 3.0.1-01(迄今为止的最新版本)可以解决问题,并使警告消息在构建过程中消失。所以,询问或寻找新版本绝对是解决这个问题的方法。
【解决方案2】:

从Java update 9开始,出现“发生了非法反射访问操作”的警告。

当我从 jdk1.8 升级到 jdk 9+(jdk11 有更多支持)时,我通过在多个项目中修改我的 pom.xml 文件,从 Maven Build 和 Maven Install 解决了这个问题。以下是消除“发生非法反射访问操作”构建消息的 2 个示例:

从以下位置更改版本:

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

收件人:

<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(干净编译包)或Maven Install时,“发生了非法反射访问操作”消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2021-08-29
    相关资源
    最近更新 更多