Java 如何添加 jar 文件并使用命令提示符进行编译?(错误:发生 JNI 错误,请检查您的安装)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32034444/
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
How to add jar files and compile using command prompt? (Error: A JNI error has occured,Please Check your Installation)
提问by Pratik
I forked one project from github and when i want to run it gives error. I added org.json
jar file to jdk\lib
and also set the Path variable to it. I compiled code but it got no errors but when i run it i get exception as follows : (and I am running it using command prompt)
我从 github 分叉了一个项目,当我想运行它时出错。我向其中添加了org.json
jar 文件,jdk\lib
并为其设置了 Path 变量。我编译了代码,但没有错误,但是当我运行它时,我得到如下异常:(我使用命令提示符运行它)
Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONExceptio n at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.privateGetMethodRecursive(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod(Unknown Source) at sun.launcher.LauncherHelper.validateMainClass(Unknown Source) at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) Caused by: java.lang.ClassNotFoundException: org.json.JSONException at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source)... 7 more
Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONExceptio n at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.privateGetMethodRecursive(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod(Unknown Source) at sun.launcher.LauncherHelper.validateMainClass(Unknown Source) at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) Caused by: java.lang.ClassNotFoundException: org.json.JSONException at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source)……还有 7 个
采纳答案by ankhzet
Add required jar to classpath like this:
将所需的 jar 添加到类路径,如下所示:
For *nix-like systems:
export CLASSPATH=<path-to-app>/myapp.jar:<path-to-app>/lib/json.jar:. java MyApp
For Windows:
set CLASSPATH=<path-to-app>/myapp.jar;<path-to-app>/lib/json.jar;. java MyApp
对于 *nix 类系统:
export CLASSPATH=<path-to-app>/myapp.jar:<path-to-app>/lib/json.jar:. java MyApp
对于 Windows:
set CLASSPATH=<path-to-app>/myapp.jar;<path-to-app>/lib/json.jar;. java MyApp
...or via manifest:
...或通过清单:
Manifest-Version: 1.0
Main-Class: MyApp
Class-Path: /lib/json.jar