I m following this tutorial : https://scrapingant.com/blog/web-scraping-java to scrap with java.
I created a java project and when I try to execute this code from the tutorial :
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class App {
public static void main(String[] args) throws IOException {
final App scraper = new App();
final String htmlContent = scraper.getContent();
final String extractedTitle = scraper.extractTitle(htmlContent);
System.out.println(extractedTitle);
}
private String getContent() throws IOException {
final OkHttpClient client = new OkHttpClient.Builder().build();
final String urlToScrape = "https://example.com";
final Request request = new Request.Builder().url(urlToScrape).build();
final Response response = client.newCall(request).execute();
return response.body().string();
}
private String extractTitle(String content) {
final Pattern titleRegExp = Pattern.compile("<title>(.*?)</title>", Pattern.DOTALL);
final Matcher matcher = titleRegExp.matcher(content);
matcher.find();
return matcher.group(1);
}
}
but i get this error :
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at okhttp3.ConnectionPool.<init>(ConnectionPool.kt)
at okhttp3.ConnectionPool.<init>(ConnectionPool.kt:47)
at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.kt:471)
at demo.src.main.java.com.example.App.getContent(App.java:20)
at demo.src.main.java.com.example.App.main(App.java:14)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 5 more
I can't find why and fix this error.
To import okhttp i had to download a jar file from maven central (4.9.3) and then in vscode : java project -> add the jar file to the Referenced Libraries.
I am a beginner in Java and want to clarify that I am not particularly attached to okhttp. If another simpler/efficient library exists, I'm not against it. I just want to be able to scrape in java and then integrate the code into an android application !
Thanks in advance