0

I am getting "A JNI error has occurred, please check your installation and try again" error Whenever I'm running my java jar using "java -jar filename.jar" command with windows power shell. It works fine whenever I opens it with double click.

How Can I resolve this issue?

error screenshot

Thanks in advance.

  • 2
    You've compiled your Java code to target Java 11, but you're trying to run it in a Java 8 runtime. You need to make sure your runtime version is at least as new as your target version. – Michael Jul 30 '20 at 11:17

2 Answers2

1

As it says quite clearly in the error message, your JRE is too old. Install a newer JRE (and update your PATH and JAVA_HOME).

Also, please do not post error messages as screenshots when you could also paste them as text.

1

You say it works when you double click the JAR file (in File Explorer) but not when you run it from the PowerShell window in this folder:

C:\incubating-netbeans-11.0-bin\netbeans\ani\Tank-IQ-Display-Configurator\Tank-IQ-Display-Configurator\dist

I'm guessing you have more than one JDK installed.

If you enter the following command in the PowerShell window, it will display the paths to all the java.exe files.

where.exe java

Windows associates file extensions with executables. Obviously, on your computer, the .jar extension is associated with java.exe. You can check this via Control Panel. Look for Default Apps. Hence when you double click the JAR in File Explorer the associated executable is launched.

I'm guessing that the default executable is that of JDK 11 and that either in the folder whose path I wrote above there is a java.exe that is compatible with JDK 8 or in your PATH environment variable, the path to the JDK 8 executable comes before the path to JDK 11 executable.

So check those things, i.e.

  1. Default apps in Windows
  2. PATH environment variable

Obviously there are many different ways to rectify the situation. I don't think any one is clearly superior and the most appropriate would depend on your environment and your needs which I am unaware of since you haven't provided those details, hence I won't suggest what actions you should take in order to resolve your issue.

What you do need to do is ensure that a JAR file containing java code that was compiled to JDK 11 is run with a java.exe from at least JDK 11.

Note that higher java versions can run classes compiled to lower versions. In other words, if your JAR was compiled to JDK 8, you could run it with JDK 11, but not the other way around (which is the cause of your error, as others have indicated).

Abra
  • 19,142
  • 7
  • 29
  • 41
  • You are right. I can see two jdk in my system. One is in Program Files & other is in Program Files(x86). – Kumar Anil Chaurasiya Jul 30 '20 at 12:25
  • How can I make java 11 as a default jdk? – Kumar Anil Chaurasiya Jul 30 '20 at 12:26
  • @KumarAnilChaurasiya make sure that JDK 11 appears before all others in the PATH environment variable. Also make sure it appears before the current directory, i.e. the `.` in the PATH variable. Also setting JAVA_HOME environment variable may also help although I would say you don't have to. – Abra Jul 30 '20 at 12:34