Google News
logo
Jsoup - Interview Questions
How do you include Jsoup in your Java project?
To include Jsoup in your Java project, you typically need to follow these steps:

1. Download Jsoup JAR file : First, you need to download the Jsoup JAR file from the official Jsoup website or a repository like Maven Central. You can download the latest version or choose a specific version based on your project requirements.

2. Add Jsoup JAR to your project's classpath :  If you're using a build tool like Maven, Gradle, or Apache Ivy, you can add Jsoup as a dependency in your project configuration file (pom.xml for Maven, build.gradle for Gradle, etc.). Here's an example of adding Jsoup dependency in Maven:
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.14.3</version> <!-- Replace with the desired version -->
</dependency>?

If you're not using a build tool, you can manually add the Jsoup JAR file to your project's classpath by copying it to a directory within your project and configuring your IDE or build script to include it.

3. Import Jsoup classes : Once Jsoup is added to your project's classpath, you can import Jsoup classes in your Java code using the import statement. For example:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;?

4. Start using Jsoup : You can now start using Jsoup in your Java code to parse HTML documents, manipulate HTML elements, extract data, and perform other HTML-related tasks.

Here's a simple example demonstrating how to use Jsoup to parse an HTML document from a URL:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try {
            // Parse HTML from a URL
            Document doc = Jsoup.connect("http://example.com").get();
            
            // Print the title of the HTML document
            System.out.println("Title: " + doc.title());
            
            // Print the HTML content
            System.out.println("HTML: " + doc.html());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}?

Make sure to replace "http://example.com" with the URL of the HTML document you want to parse.
Advertisement