Android java.lang.NoClassDefFoundError: org.jsoup.Jsoup
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9934744/
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
Android java.lang.NoClassDefFoundError: org.jsoup.Jsoup
提问by red
I work with eclipse Version: Indigo Service Release 2 Build id: 20120216-1857. The Android version ist 2.2. I make an app to test a connect and parse a web site like this:
我使用 eclipse 版本:Indigo Service Release 2 Build id:20120216-1857。Android 版本是 2.2。我制作了一个应用程序来测试连接并解析这样的网站:
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Document doc = Jsoup.connect("http://example.com/").get();
Elements divs = doc.select("div#test");
for (Element div : divs) {
System.out.println(div.text());
}
} catch (Exception e) {
}
}
}
Manifest file:
清单文件:
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TestActivity"
android:label="@string/app_name"
android:configChanges="orientation"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<uses-library android :name="org.jsoup.Jsoup" />
</activity>
</application>
I add the library jsoup-1.6.1.jar to JAVA Buildpath, but I become the runtime error: E/AndroidRuntime(736): java.lang.NoClassDefFoundError: org.jsoup.Jsoup
我将库 jsoup-1.6.1.jar 添加到 JAVA Buildpath,但我变成了运行时错误:E/AndroidRuntime(736): java.lang.NoClassDefFoundError: org.jsoup.Jsoup
How can I resolve this error?
我该如何解决这个错误?
回答by John O'Connor
I encountered this exact problem after a recent update of the ADT component of Android. It looks like Android is ignoring the build path and instead using the ANT defaults.
在最近更新 Android 的 ADT 组件后,我遇到了这个确切的问题。看起来 Android 正在忽略构建路径,而是使用 ANT 默认值。
I solved this by removing my jar file from the build path, creating a folder in the "root" of the app heirarchy (alongside src, gen, etc) called "libs", and putting my .jar there. When you clean / build and re-launch the app, the error should go away.
我通过从构建路径中删除我的 jar 文件来解决这个问题,在应用程序层次结构的“根”(与 src、gen 等一起)创建一个名为“libs”的文件夹,并将我的 .jar 放在那里。当您清理/构建并重新启动应用程序时,错误应该会消失。
FYI - this is occurring because the JAR file is not getting packaged into the .apk files - this is why it builds correctly, but isn't available during run-time on your Android device.
仅供参考 - 这是因为 JAR 文件没有被打包到 .apk 文件中 - 这就是它正确构建的原因,但在您的 Android 设备上运行时不可用。
回答by mmaitlen
There's also the issue that jars have to be in the libs (with an 's') directory and not lib....could this be your problem?
还有一个问题,jar 必须在 libs(带有“s”)目录中,而不是 lib....这可能是你的问题吗?
回答by Katana24
You don't need the line:
您不需要该行:
<uses-library android:name = "org.jsoup.Jsoup"/>
in your manifest file. All you need to do to use Jsoup is to ensure it's part of your build path by doing the following:
在您的清单文件中。使用 Jsoup 所需要做的就是通过执行以下操作来确保它是构建路径的一部分:
- Right click on your project and select Properties
- Select Java Build Pathand select Add External JARs...
- Navigate to the Jsoup jar file
- 右键单击您的项目并选择属性
- 选择Java 构建路径并选择添加外部 JAR...
- 导航到 Jsoup jar 文件
Then, Jsoup will act like any other library in java. For example in my app I use it like so:
然后,Jsoup 将像 Java 中的任何其他库一样工作。例如在我的应用程序中,我像这样使用它:
try
{
Document doc = Jsoup.connect( myHtml ).get();
Elements table = doc.select( "table#ctl00_ContentPlaceHolder1_tblPasses" );
// Returns first row of the table which we want
Elements row = table.select( "tr.lightrow" );
Element record_One = row.first();
}
Note - I have not included all my code for it because it's rather long. Anyway, after that you just import the classes as needed, for example in mine I use the following:
注意 - 我没有包含我所有的代码,因为它很长。无论如何,之后您只需根据需要导入类,例如在我的中我使用以下内容:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
回答by Antesh Sharma
- Right click on the project name > Properties > Java Build Path > tab Libraries then click on button Add External jars.
- select jar's path from your directory where you had downloaded jsoup-1.7.3.jar.
- After adding jar go to next tab Order and export and select checkbox jsoup-1.7.3.jar 4 click ok 5.clean build your project and then run.
- 右键单击项目名称 > 属性 > Java 构建路径 > 选项卡库,然后单击按钮添加外部 jar。
- 从您下载 jsoup-1.7.3.jar 的目录中选择 jar 的路径。
- 添加 jar 后转到下一个选项卡 Order and export 并选择复选框 jsoup-1.7.3.jar 4 单击确定 5.clean 构建您的项目,然后运行。
I solved the same problem by following above steps
我按照上述步骤解决了同样的问题
回答by justinleona
I was able to solve this problem by following:
我能够通过以下方式解决这个问题:
- Placing the jsoup jar in the 'libs' folder (as noted in John's answer)
- Remove the "Android Dependencies" and "Android Private Libraries" from the build path
- Using Android Tools > Fix Project Properties to repopulate the above entries in the build path.
- 将 jsoup jar 放在“libs”文件夹中(如约翰的回答中所述)
- 从构建路径中删除“Android 依赖项”和“Android 私有库”
- 使用 Android 工具 > 修复项目属性在构建路径中重新填充上述条目。
回答by Imran Jawaid
You are doing it wrong. You cant use Jsoup functions in the oncreate of Activity. You need to make either Async Task or thread.
你做错了。您不能在 Activity 的 oncreate 中使用 Jsoup 函数。您需要创建异步任务或线程。
This is to prevent doing blocking tasks in you activity.
这是为了防止在您的活动中执行阻塞任务。
Here is a nice blog that explain how to do it and provide sample code as well.
这是一个很好的博客,它解释了如何做到这一点并提供了示例代码。
回答by Soheil
Just close the Eclipse and open it again. The problem will be solved.
只需关闭 Eclipse 并再次打开它。问题将得到解决。
回答by deadfish
My solution was
我的解决方案是
- Right click on the project name > Properties > Java Build Path > tab Libraries > remove everything except the "Android X.X" (2.3.3 in my case) and the "Android Dependencies" (according to link found by @KarlKarlsom >android.foxykeep.com<from stack problem >stack<)
- I created folder libs in my main projects. As I was using jsoup so I moved it to this folder
- Restarted eclipse, clean up project didn't work
- 右键单击项目名称 > 属性 > Java 构建路径 > 选项卡库 > 删除除“Android XX”(在我的情况下为 2.3.3)和“Android 依赖项”(根据 @KarlKarlsom >android.foxykeep找到的链接)之外的所有内容.com<来自堆栈问题>stack<)
- 我在我的主要项目中创建了文件夹库。因为我使用的是 jsoup 所以我把它移到了这个文件夹
- 重启eclipse,清理项目没用
p.s I was using jsoup and sherlock. I didn'y change sherlock library, only my main project.
ps我使用的是jsoup和sherlock。我没有改变夏洛克图书馆,只有我的主要项目。