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

时间:2018-03-15 11:29:45

标签: java maven opencv java-9 javacv

我想创建一个可移植的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>

它适用于以下警告 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:以及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。

2 个答案:

答案 0 :(得分:0)

这是因为Java 9对非法访问进行了新的检查,并且在java 9发布后常常会出现这种安全警告。永久解决方案是向维护者报告错误并等待它们发布补丁更新。

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

答案 1 :(得分:0)

自Java 9更新以来,出现“发生了非法的反射访问操作”警告。

尝试替换Maven编译插件。当我确实按照以下示例从jdk1.8升级到jdk1.12时,通过在多个项目中修改pom.xml文件,已解决了Maven Build和Maven Install的问题:

更改版本:

{
  "request": {
    "city": "New York"
  }
}

收件人:

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

并且还更改了artifactId和版本自:

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

收件人:

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

当我重新运行Maven Build或Maven Install时,“发生了非法的反射访问操作”