繁体   English   中英

警告:发生了非法的反射访问操作(java 中的便携式 opencv)

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

我想制作一个可移植的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 显示所有库都呈灰色,如下所示:

opencv 3.2 的 maven 依赖文件夹

如果我忽略此警告并且不将库与警告消息中提到的命令链接怎么办? 因为用java使opencv可移植非常简单,我想意识到如果没有问题,那么以后继续这种方法。

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

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

这是因为 Java 9 对非法访问进行了新的检查,并且在 Java 9 发布后经常出现此类安全警告。对此的永久解决方案是将错误报告给维护者并等待他们发​​布一个补丁更新。

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

自 Java 更新 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 和 version:

<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时,“发生非法反射访问操作”消失了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM