jsoup - Loading from File

Following example will showcase fetching an HTML from the disk using a file and then find its data.


String url = "http://www.google.com";
Document document = Jsoup.connect(url).get();


  • document − document object represents the HTML DOM.

  • Jsoup − main class to connect the url and get the HTML String.

  • url − url of the html page to load.


The connect(url) method makes a connection to the url and get() method return the html of the requested url.


Create the following java program using any editor of your choice in say C:/> jsoup.


import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

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

public class JsoupTester {
   public static void main(String[] args) throws IOException, URISyntaxException {
      URL path = ClassLoader.getSystemResource("test.htm");
      File input = new File(path.toURI());
      Document document = Jsoup.parse(input, "UTF-8");


Create following test.htm file in C:\jsoup folder.

      <title>Sample Title</title>
      <p>Sample Content</p>

Verify the result

Compile the class using javac compiler as follows:

C:\jsoup>javac JsoupTester.java

Now run the JsoupTester to see the result.

C:\jsoup>java JsoupTester

See the result.

Sample Title