Java:NoClassDefFoundError:org/json/JSONException [英] Java: NoClassDefFoundError: org/json/JSONException

查看:167
本文介绍了Java:NoClassDefFoundError:org/json/JSONException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我整天都在解决这个问题.
我确定我使用的是正确的类路径.
另外,我还有其他软件包作为依赖项,它们可以很好地工作.

So, I've spent the whole day with this problem.
I'm sure, that I'm using correct classpath.
Also, I have other packages as dependences, and they work perfectly.

我有一个使用org.json的类.*
另外,此类中还使用了其他一些外部软件包.
所有这些依赖性都作为jar文件放置在我的/path/to/libs/中.
json-20160212.jar就在其中.

I have a class that uses org.json.*
Also there are some other outer packages used in this class.
All this dependences are placed as jar files in my /path/to/libs/.
json-20160212.jar is among them.

我正在使用

javac \
    -cp "src/:/path/to/libs/json-20160212.jar:/path/to/libs/other.jar:/path/to/libs/another.jar" \
    -d dst/ \
    src/com/example/Source.java

编译没有问题.
然后,我从我的类文件创建jar.
清单:

Compilation goes without issues.
Then, I'm creating jar from my class-files.
Manifest:

Main-Class: com.example.Source
Class-Path: /path/to/libs/json-20160212.jar
  /path/to/libs/other.jar
  /path/to/libs/another.jar

命令行:
jar cfm output.jar manifest -C dst/ ./com

我要把这个清单放在罐子里

I'm getting jar with this manifest:

Manifest-Version: 1.0
Class-Path: /path/to/libs/json-20160212.jar /path/to/libs/other.jar /p
 ath/to/libs/another.jar
Created-By: 1.7.0_101 (Oracle Corporation)
Main-Class: com.example.Source

据我了解,对于已编译的清单包含分行的行是可以的.

As I've understood, this is ok for compiled manifest to have splitted lines.

现在,我正在从命令行运行我的应用程序,并收到此错误:

Now, I'm running my app from command line and get this error:

Exception in thread "Thread-0" java.lang.NoClassDefFoundError: org/json/JSONException
    at com.example.Source.run(Source.java:30)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

据我所知,这意味着org.json.JSONException在编译时可以正常运行,但在运行时会丢失.
但是我该怎么办?
我有那个文件.在编译过程中和运行时都处于该位置.
另外还存在其他依赖项,它们的jar也位于该位置.

As I know, that means, that org.json.JSONException was ok at compile time but missing at run time.
But what must I do with this info?
I have that file. It was at its place during compilation and at runtime.
Also there are other dependences, and their jars are also at that place.

如果我从应用程序中删除JSON使用情况,则一切正常.
因此,我可以得出结论,正是软件包org.json本身导致了问题.

If I remove JSON usage from my app, everything is working ok.
So, I can make conclusion, that it is the package org.json itself, that makes the problem.

我该怎么做才能使其正常工作?

What must I do, to make it work?

更新

现在,我进行了以下更改:

Now, I've made this changes:

我的目录结构:

libs/
    json-20160212.jar
    other.jar
    another.jar
src/
    com/
        example/
            Source.java
dst/

清单:

Main-Class: com.example.Source
Class-Path: libs/json-20160212.jar
  libs/other.jar
  libs/another.jar

编译:

javac \
    -cp "src/:libs/json-20160212.jar:libs/other.jar:libs/another.jar" \
    -d dst/ \
    src/com/example/Source.java

归档:

jar cfm dst/output.jar manifest -C dst/ ./com ./libs

我正在使用具有以下结构的jar:

I'm getting jar with the structure as excepted:

META-INF/
META-INF/MANIFEST.MF
com/
com/example/
com/example/Source.class
libs/
libs/json-20160212.jar
libs/other.jar
libs/another.jar

我正在用java -jar dst/output.jar运行它.
结果是相同的:java.lang.NoClassDefFoundError: org/json/JSONException

And I'm running it with java -jar dst/output.jar.
Result is the same: java.lang.NoClassDefFoundError: org/json/JSONException

推荐答案

因此,解决方案:

据我了解,访问jar内jar文件内容的唯一方法是编写自己的类加载器.
没有它,必须提取jar文件,并且提取的内容必须包含在output.jar中.

As I've understand, the only way to access the content of the jar files that are inside your jar, is to write your own class loader.
Without it, jar files must be extracted and that extracted content must be included to output.jar.

这篇关于Java:NoClassDefFoundError:org/json/JSONException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆