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

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

How to add jar files and compile using command prompt? (Error: A JNI error has occured,Please Check your Installation)

javaexceptionnoclassdeffounderrorjsonexception

提问by Pratik

I forked one project from github and when i want to run it gives error. I added org.jsonjar file to jdk\liband 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.jsonjar 文件,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 添加到类路径,如下所示:

  1. For *nix-like systems:

    export CLASSPATH=<path-to-app>/myapp.jar:<path-to-app>/lib/json.jar:.
    java MyApp
    
  2. For Windows:

    set CLASSPATH=<path-to-app>/myapp.jar;<path-to-app>/lib/json.jar;.
    java MyApp
    
  1. 对于 *nix 类系统:

    export CLASSPATH=<path-to-app>/myapp.jar:<path-to-app>/lib/json.jar:.
    java MyApp
    
  2. 对于 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