英文:
Error occurred when running with Windows power shell. A JNI error has occurred, please check your installation and try again
问题
我遇到了“发生了JNI错误,请检查您的安装并重试”错误,每当我在Windows PowerShell中使用“java -jar filename.jar”命令运行我的Java JAR文件时都会出现此错误。但是,每当我双击打开它时,它可以正常运行。
我该如何解决这个问题?
提前感谢您的帮助。
英文:
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?
Thanks in advance.
答案1
得分: 1
你的JRE版本太旧,正如错误信息中明确提到的那样。安装一个更新的JRE(并更新你的PATH和JAVA_HOME)。
另外,请不要将错误消息以截图的方式发布,而是可以将它们粘贴为文本。
英文:
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.
答案2
得分: 1
你说当你在文件资源管理器中双击JAR文件时它可以工作,但是当你在此文件夹的PowerShell窗口中运行它时就不行了:
C:\incubating-netbeans-11.0-bin\netbeans\ani\Tank-IQ-Display-Configurator\Tank-IQ-Display-Configurator\dist
我猜你安装了多个JDK。
如果你在PowerShell窗口中输入以下命令,它将显示所有java.exe
文件的路径。
where.exe java
Windows将文件扩展名与可执行文件关联。显然,在你的计算机上,.jar
扩展名与java.exe
关联。你可以通过"控制面板"来检查这一点。寻找"默认应用程序"。因此,当你在文件资源管理器中双击JAR文件时,关联的可执行文件会被启动。
我猜默认的可执行文件是JDK 11的,要么在我上面写的文件夹中有一个与JDK 8兼容的java.exe
,要么在你的PATH环境变量中,JDK 8可执行文件的路径出现在JDK 11可执行文件的路径之前。
所以检查这些事情,即:
- Windows中的默认应用程序
- PATH环境变量
显然,有很多不同的方法来纠正这种情况。我认为没有一种方法明显优越,最合适的方法会取决于你的环境和需求,因为我不了解这些细节,所以我不会建议你采取什么行动来解决你的问题。
你需要确保运行编译为JDK 11的java代码的JAR文件至少使用来自JDK 11的java.exe
运行。
请注意,更高版本的Java可以运行编译为较低版本的类。换句话说,如果你的JAR文件是使用JDK 8编译的,你可以使用JDK 11运行它,但反之则不行(这就是你的错误原因,正如其他人所指出的那样)。
英文:
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.
- Default apps in Windows
- 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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论