How do I add jsoup to my Java project?

Adding jsoup (Java HTML Parser) to your Java project is straightforward with several methods available. Choose the approach that best fits your project setup and development environment.

Maven Integration

For Maven projects, add jsoup as a dependency in your pom.xml file within the <dependencies> section:

<dependencies>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.17.2</version>
    </dependency>
</dependencies>

Maven will automatically download jsoup and handle all dependencies during the build process. Run mvn clean install to download the library.

Gradle Integration

For Gradle projects, add the dependency to your build.gradle file:

Gradle (Groovy DSL)

dependencies {
    implementation 'org.jsoup:jsoup:1.17.2'
}

Gradle (Kotlin DSL)

dependencies {
    implementation("org.jsoup:jsoup:1.17.2")
}

Run ./gradlew build or sync your project to download the dependency.

SBT (Scala Build Tool)

For SBT projects, add to your build.sbt file:

libraryDependencies += "org.jsoup" % "jsoup" % "1.17.2"

Manual JAR Installation

If you're not using a build management tool, download and add the JAR manually:

Step-by-Step Process

  1. Download jsoup: Visit jsoup.org/download and download the latest JAR file
  2. Create lib directory: Create a lib folder in your project root if it doesn't exist
  3. Place JAR file: Copy the downloaded jsoup-1.17.2.jar to the lib directory

IDE Configuration

IntelliJ IDEA

  1. File → Project Structure → Modules
  2. Select Dependencies tab
  3. Click "+" → JARs or directories
  4. Navigate to and select the jsoup JAR file
  5. Click Apply and OK

Eclipse

  1. Right-click project → Properties
  2. Java Build Path → Libraries tab
  3. Click "Add JARs..." or "Add External JARs..."
  4. Select the jsoup JAR file
  5. Click Apply and Close

Visual Studio Code

  1. Create .vscode/settings.json in your project root
  2. Add the following configuration:
{
    "java.project.referencedLibraries": [
        "lib/**/*.jar"
    ]
}

Command Line Usage

Compilation

# Linux/macOS
javac -cp ".:lib/jsoup-1.17.2.jar" YourJavaFile.java

# Windows
javac -cp ".;lib\jsoup-1.17.2.jar" YourJavaFile.java

Execution

# Linux/macOS
java -cp ".:lib/jsoup-1.17.2.jar" YourMainClass

# Windows
java -cp ".;lib\jsoup-1.17.2.jar" YourMainClass

Quick Start Example

Once jsoup is added to your project, verify the installation with this simple test:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class JsoupTest {
    public static void main(String[] args) {
        try {
            // Parse HTML from URL
            Document doc = Jsoup.connect("https://example.com").get();

            // Extract page title
            String title = doc.title();
            System.out.println("Page title: " + title);

            // Find first paragraph
            Element firstP = doc.selectFirst("p");
            if (firstP != null) {
                System.out.println("First paragraph: " + firstP.text());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Version Management

Finding Latest Version

Version Compatibility

  • jsoup 1.17.x: Requires Java 8 or higher
  • jsoup 1.15.x and below: Compatible with Java 7+
  • Always check the changelog for breaking changes

Troubleshooting

Common Issues

  • ClassNotFoundException: Verify jsoup JAR is in classpath
  • Version conflicts: Check for duplicate jsoup versions in dependencies
  • Build failures: Ensure correct repository configuration in build files

Dependency Analysis

Use these commands to verify jsoup inclusion:

# Maven
mvn dependency:tree | grep jsoup

# Gradle
./gradlew dependencies | grep jsoup

Choose the installation method that matches your project's build system for the best development experience.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon