新的 JSONObject 触发器 java.lang.NoClassDefFoundError

new JSONObject triggers java.lang.NoClassDefFoundError

我正在使用 JDeveloper IDEJava 语言为 OAM(Oracle Access Manager) 开发自定义身份验证插件[= =92=].


我正在解析 URL 并且我从 JSONObject 正确地 Main.class 文件 没有 触发任何类型的Exception

这让我得出 假设,即整个 解析代码是 正确的 这意味着readJsonFromUrl 函数 起作用了。


让我提一下我的 PhillPlugin.class 包括什么

  1. public ExecutionStatus process(AuthenticationContext context) , 当插件为 运行.
  2. 时触发
  3. public void getDataGenerate(String Url),调用内部 process 函数从 URL
  4. 创建 JSONObject
  5. public static JSONObject readJsonFromUrl(String url)getDataGenerate 函数中调用
  6. private static String readAll(Reader rd)用于内部解析readJsonFromUrl

现在我将插件上传到服务器,我 运行 它并在它的日志中得到以下内容

java.lang.NoClassDefFoundError: org/json/JSONObject
    at phillplugin.PhillPlugin.readJsonFromUrl(PhillPlugin.java:184)
    at phillplugin.PhillPlugin.getDataGenerate(PhillPlugin.java:132)
    at phillplugin.PhillPlugin.process(PhillPlugin.java:63)

创建插件需要什么:

  • PhillPlugin.class
  • PhillPlugin.xml
  • MANIFEST.MF

我提到上面的内容是因为我必须在这些文件的某处包含 org.json 路径。 (它已经作为导入存在于 PhillPlugin.classMain.class 中)

The org.json.jar is included in Project's Libraries as well as all the .jars in order to build the Plug-In


MANIFEST.MF

Manifest-Version: 1.0
Bundle-Version: 10
Bundle-Name: PhillPlugin
Bundle-Activator: phillplugin.PhillPlugin
Bundle-ManifestVersion: 2
Import-Package: org.osgi.framework;version="1.3.0",oracle.security.am.plugin,oracle.security.am.plugin.authn,oracle.security.am.plugin.impl,oracle.security.am.plugin.api,oracle.security.am.common.utilities.principal,oracle.security.idm,javax.security.auth
Bundle-SymbolicName: PhillPlugin
CLASSPATH: felix.jar, identitystore.jar, oam-plugin.jar, utilities.jar, org.json.jar

Sample of the PhillPlugin.Class

出于安全考虑,我不应该包含 URL。 (相信我这是有效的)

    public void getDataGenerate(String Url) {
        System.out.println("-----   Reading Json Object  -----");
                       JSONObject json;
        try {
            json = readJsonFromUrl(Url);
            System.out.println("The Json Object: "+json.toString());
            otp=Integer.parseInt((String) json.get("otp"));
            System.out.println("The User is:"+user+"\n"+"His OTP is: "+otp);
        } catch (Exception e) {
            System.out.println("Exception : "+e.toString());
        }          

    public static JSONObject readJsonFromUrl(String url) throws IOException,JSONException {
        System.out.println("Opening Stream");
        InputStream is = new URL(url).openStream();
        System.out.println("Stream opened");
        try {
         System.out.println("----------\n\n\nUrl to Parse: "+url+"\n\n\n");
          BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
          System.out.println("\n\n\n"+"BufferedReader opened\n\n\n\n");
          String jsonText =(String) readAll(rd);
          System.out.println("\n\n\nJsonTEXT:"+jsonText+"\n\n\n");
            JSONObject json=null;
            System.out.println("\n\n Created Json Instance\n\n\n");
            try{
                System.out.println("inside try statement - initializing JSONObject with the text above \n\n\n");
             //-------ERROR TRIGGERED HERE---------
             json = new JSONObject(jsonText);
                System.out.println("--------------------Object created-------------------");
            }catch (Exception e) {
                System.out.println("\n\n\n\n\nJSONOBJECT failed to be created: \n"+e);
            }
          System.out.println("\n\n\nJSON OBJECT"+json+"\n\n\n\n");
          return json;
        } finally {
          is.close();
        }

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
          sb.append((char) cp);
        }
        return sb.toString();
      }

PhillPlugin.xml

<Plugin type="Authentication">
    <author>uid=Phill</author>
    <email>phill@oracle.com</email>
    <creationDate>12:47:00, 2019-07-11</creationDate>
    <description>Phill-Plugin Prints Hello</description>
    <configuration>
    </configuration>
</Plugin>


这是服务器日志崩溃前的输出:


Stream opened
----------
Url to Parse: https://something

BufferedReader opened

JsonTEXT: it's correct

Created Json Instance

inside try statement - initializing JSONObject with the text above 

I'm worrying too much about the MANIFEST.MF file because probably i'm doing something wrong in there

抱歉这么久 post,如果需要,我会提供任何额外信息,谢谢

MANIFEST.MF中的CLASSPATH条目显然是错误的。正确名称是 Class-Path 参见:https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

整个 MANIFEST.MF 文档:https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

OAM 服务器编写 Authenticaiton Plugin 时,所有额外的库都应在 MANIFEST.MF 以及外部 .jar 文件中提及。

此外,所有 .jar 文件应部署在最终插件 .jar 以及外部 libraries

在我的例子中,我必须在导出的 PhillPlugin.jar 中包含 org.json.jar,如下所示:

  • PhillPlugin.jar

如您所见,org.json.jar 及其库 org 都是必需的

  • Manifest.MF

最后一步是在 MANIFEST.MF 中提及您在插件中使用的所有额外 classes

在我的例子中,我必须将它包含在我的 Import-Package 属性中,以便能够创建一个 JSONObject 实例

org.json;resolution:=optional,
org.json.JSONObject;resolution:=optional

如果您想使用 JSONArray,您必须添加:

org.json.JSONArray;resolution:=optional

等等。

编辑: class-path 应该提到如下:

Bundle-ClassPath: org.json.jar, felix.jar, identity-provider.jar, oam-plugin.jar, utilities.jar