简体   繁体   English

Android java.lang.NoClassDefFoundError:org.jsoup.Jsoup

[英]Android java.lang.NoClassDefFoundError: org.jsoup.Jsoup

I work with eclipse Version: Indigo Service Release 2 Build id: 20120216-1857. 我使用eclipse版本:Indigo Service Release 2 Build id:20120216-1857。 The Android version ist 2.2. Android版本是2.2。 I make an app to test a connect and parse a web site like this: 我创建了一个应用来测试连接并解析一个这样的网站:

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? 我该如何解决这个错误?

I encountered this exact problem after a recent update of the ADT component of Android. 最近更新了Android的ADT组件后,我遇到了这个确切的问题。 It looks like Android is ignoring the build path and instead using the ANT defaults. 看起来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. 我通过从构建路径中删除我的jar文件解决了这个问题,在应用程序heirarchy的“根”中创建了一个文件夹(与src,gen等一起),称为“libs”,并将我的.jar放在那里。 When you clean / build and re-launch the app, the error should go away. 当您清理/构建并重新启动应用程序时,错误应该消失。

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设备的运行时无法使用。

see NoClassDefFoundError - Eclipse and Android 请参阅NoClassDefFoundError - Eclipse和Android

还有一个问题,jar必须在libs(带有's')目录而不是lib ....这可能是你的问题吗?

  1. Right click on the project name > Properties > Java Build Path > tab Libraries then click on button Add External jars. 右键单击项目名称> Properties> Java Build Path>选项卡Libraries,然后单击Add External jars按钮。
  2. select jar's path from your directory where you had downloaded jsoup-1.7.3.jar. 从您下载jsoup-1.7.3.jar的目录中选择jar的路径。
  3. 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. 添加jar后转到下一个选项卡Order并导出并选择复选框jsoup-1.7.3.jar 4单击ok 5.clean构建项目然后运行。

I solved the same problem by following above steps 我按照上面的步骤解决了同样的问题

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 Path and select Add External JARs... 选择Java Build Path并选择Add External JARs ...
  • Navigate to the Jsoup jar file 导航到Jsoup jar文件

Then, Jsoup will act like any other library in java. 然后,Jsoup将像java中的任何其他库一样运行。 For example in my app I use it like so: 例如在我的应用程序中我使用它如下:

    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;

You are doing it wrong. 你做错了。 You cant use Jsoup functions in the oncreate of Activity. 你不能在Activity的oncreate中使用Jsoup函数。 You need to make either Async Task or thread. 您需要创建异步任务或线程。

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. 这是一个很好的博客,解释了如何做到这一点并提供示例代码。

Sample Code Click here 示例代码点击此处

I was able to solve this problem by following: 我能够通过以下方式解决这个问题:

  1. Placing the jsoup jar in the 'libs' folder (as noted in John's answer) 将jsoup jar放在'libs'文件夹中(如John的回答中所述)
  2. Remove the "Android Dependencies" and "Android Private Libraries" from the build path 从构建路径中删除“Android依赖项”和“Android私有库”
  3. Using Android Tools > Fix Project Properties to repopulate the above entries in the build path. 使用Android工具>修复项目属性重新填充构建路径中的上述条目。

Just close the Eclipse and open it again. 关闭Eclipse并再次打开它。 The problem will be solved. 问题将得到解决。

My solution was 我的解决方案是

  1. Right click on the project name > Properties > Java Build Path > tab Libraries > remove everything except the "Android XX" (2.3.3 in my case) and the "Android Dependencies" (according to link found by @KarlKarlsom >android.foxykeep.com< from stack problem >stack< ) 右键单击项目名称>属性> Java构建路径>选项卡库>删除除“Android XX”(在我的情况下为2.3.3)和“Android依赖项”之外的所有内容(根据@KarlKarlsom找到的链接> android.foxykeep .com <来自堆栈问题>堆栈<
  2. I created folder libs in my main projects. 我在主项目中创建了文件夹库。 As I was using jsoup so I moved it to this folder 当我使用jsoup所以我把它移动到这个文件夹
  3. Restarted eclipse, clean up project didn't work 重启eclipse,清理项目没有用

ps I was using jsoup and sherlock. ps我正在使用jsoup和sherlock。 I didn'y change sherlock library, only my main project. 我没有改变sherlock库,只有我的主要项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM