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
- Download jsoup: Visit jsoup.org/download and download the latest JAR file
- Create lib directory: Create a
lib
folder in your project root if it doesn't exist - Place JAR file: Copy the downloaded
jsoup-1.17.2.jar
to thelib
directory
IDE Configuration
IntelliJ IDEA
- File → Project Structure → Modules
- Select Dependencies tab
- Click "+" → JARs or directories
- Navigate to and select the jsoup JAR file
- Click Apply and OK
Eclipse
- Right-click project → Properties
- Java Build Path → Libraries tab
- Click "Add JARs..." or "Add External JARs..."
- Select the jsoup JAR file
- Click Apply and Close
Visual Studio Code
- Create
.vscode/settings.json
in your project root - 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
- Check Maven Central Repository
- Visit jsoup.org for release announcements
- Use dependency update tools like Maven Versions Plugin or Gradle Versions Plugin
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.