Published 15 Sep, 2022

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

Category Java
Modified : Sep 29, 2022
12

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.

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:

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?

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

Answers

There are 4 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Compiler Errors, Java, Eclipse, Java 8, Java Native Interface. These have been categorized in sections for a clear and precise explanation.

17

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.


50

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.


34

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


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