Error: package org.jsoup does not exist with import org.jsoup.Jsoup;

230 views Asked by At

I've downloaded the Jsoup Java library for use in VS Code. I've clicked Referenced Libraries and added the JAR file there. I created this class to see if the imports work:

import org.jsoup.Jsoup;

public class n {
}

When I tried to compile it, I got this error:

javac n.java
n.java:1: error: package org.jsoup does not exist
import org.jsoup.Jsoup;

When I press Ctrl + T and look for some classes, I can see their implementations and all. Why would it not be able to find the package? I already tried compiling with java -cp .:"jsoup-1.17.2.jar" n.java, but it didn't work.

1

There are 1 answers

2
Tamas Csizmadia On BEST ANSWER

A Step By Step Guide For a PoC JSoup + VSCode Java Project

Based on your input I am not sure what went wrong on your setup, but here is how I made a working PoC Project in VSCode using JDK 11.

Download JSoup Jar

I assume you already did it. I downloaded JSoup 1.17.2 from mvnrepository.com.

Put it in lib directory (optional, but a good practice to separate jars from code).

Create a Sample Java Class

package com.github.tcsizmadia.jsoupsandbox;

import org.jsoup.Jsoup;

public class Main {
    public static void main(String[] args) {
        // a simple HTML document
        final var sampleHtml = "<html>\n" +
                "<head>\n" +
                "    <title>Sample HTML</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <h1>Hello World!</h1>\n" +
                "</body>\n" +
                "</html>\n";

        // parse the HTML
        var doc = Jsoup.parse(sampleHtml);
        System.out.println("The sample HTML's Title: " + doc.title());
    }
}

Save it in the corresponding directory tree: com/github/tcsizmadia/jsoupsandbox (feel free to rename packages).

Compile the Java Class

javac -cp lib/jsoup-1.17.2.jar com/github/tcsizmadia/jsoupsandbox/Main.java

Your directory tree should look like this:

├── com
│   └── github
│       └── tcsizmadia
│           └── jsoupsandbox
│               ├── Main.class
│               └── Main.java
└── lib
    └── jsoup-1.17.2.jar

Run the Compiled Java Class

java -cp .:lib/jsoup-1.17.2.jar com.github.tcsizmadia.jsoupsandbox.Main

Result:

The sample HTML's Title: Sample HTML

Please try these steps on your environment and let us know which step failed so we can narrow down the error.