Java 对于程序的一个实例,而不是另一个实例,“发生 JNI 错误”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37986783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:53:00  来源:igfitidea点击:

"A JNI Error has occurred" for one instance of the program, not for another

javaeclipsecompiler-errorsjava-8java-native-interface

提问by Robin Kramer

I made a large program in Eclipse Java Mars on one computer which worked fine. I exported the program as a runable Jar-file and running it gave no problem whatsoever. Even when I exported the entire project to another computer, the program still works.

我在一台运行良好的计算机上在 Eclipse Java Mars 中制作了一个大型程序。我将程序导出为可运行的 Jar 文件,运行它没有任何问题。即使我将整个项目导出到另一台计算机,该程序仍然有效。

The issue came forth on the other computer when I made minor changes to the project. These changes were only content based changes and should make no difference on the functionality of the program. Running the program from eclipse appears to have no problems.However, when I want to run the newly created Jar-file I get the following error:

当我对项目进行细微更改时,另一台计算机上出现了问题。这些更改只是基于内容的更改,不会对程序的功能产生影响。从 eclipse 运行程序似乎没有问题。但是,当我想运行新创建的 Jar 文件时,出现以下错误:

Error: A JNI error has occurred, please check your installation and try again. 

followed by a frame saying:

接着是一个框架说:

A Java Exception has occurred. 

So my question is:

所以我的问题是:

How is it possible that a different instances of the same program cannot run via a Jar-file, while the older instance can?

同一个程序的不同实例怎么可能不能通过 Jar 文件运行,而旧的实例可以?

I used the same JDK version (1.8.0_73) on both computers, and installed them in pretty much the same way: I first installed Java together with NetBeans via a combined release. Following, I installed Eclipse. The only difference is that on the new computer I installed eclipse offline, i.e. without the Eclipse installer

我在两台计算机上使用相同的 JDK 版本 (1.8.0_73),并以几乎相同的方式安装它们:我首先通过组合版本将 Java 与 NetBeans 一起安装。接下来,我安装了 Eclipse。唯一的区别是我在新电脑上离线安装了eclipse,即没有安装Eclipse安装程序

采纳答案by Alex Cohn

Eclipse presents different ways to export JAR for a project. To have the native library (or libraries) available when running the exported JAR, choose "package required libraries in generated JAR"option.

Eclipse 提供了为项目导出 JAR 的不同方法。要在运行导出的 JAR 时使用本机库(或多个库),请选择“在生成的 JAR 中打包所需的库”选项。

回答by loonytune

JNI means Java Native Interface, meaning the application is trying to load a native library. Those native libraries are NOT part of a compiled jar file. Try to find out what native library is needed for your application, and wheter it is installed on one but not the other machine. Also check your run configuration. Using JNI you need to set the -Djava.libary.path=<...> parameter to point to your native libraries.

JNI 表示 Java Native Interface,意味着应用程序正在尝试加载本机库。这些本机库不是已编译 jar 文件的一部分。尝试找出您的应用程序需要什么本机库,以及它是否安装在一台机器上而不是另一台机器上。还要检查您的运行配置。使用 JNI,您需要设置 -Djava.libary.path=<...> 参数以指向您的本机库。

回答by dineshkumar arivarasan

  <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.datacollector.app.DataCollectorServiceRunner</mainClass>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

回答by Jibin Philip

Please update your JDK to the latest version(JDK 11 or 12), that fix this issue.

请将您的 JDK 更新到最新版本(JDK 11 或 12),以解决此问题。