Javaでスクレイピングのプログラムを作ってみようと始めましたがビルドするとライブラリの読み込みが上手くできません。
HelloWorldと表示するだけのプログラムの場合問題なくビルド、実行できたのでライブラリが読み込めていないんじゃないかと思いました。
コードはJsoup公式のサンプルコードをコピペしてます。
https://jsoup.org/cookbook/extracting-data/example-list-links
Javaは初めてで初歩的な部分でミスしてるかと思いますがよろしくお願いします。
eclipseは使わずVScodeとMavenでやっています。
Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: /opt/homebrew/Cellar/maven/3.8.5/libexec
java 18.0.1.1 2022-04-22
HelloWorld ├── pom.xml ├── src │ └── main │ └── java │ └── HelloWorld │ └── HelloWorld.java └── target ├── HelloWorld-1.0.jar ├── archive-tmp ├── classes │ ├── HelloWorld │ │ └── HelloWorld.class │ └── org │ └── jsoup │ └── HelloWorld.class ├── generated-sources │ └── annotations ├── generated-test-sources │ └── test-annotations ├── mainclass-1.0.jar ├── maven-archiver │ └── pom.properties ├── maven-status │ └── maven-compiler-plugin │ ├── compile │ │ └── default-compile │ │ ├── createdFiles.lst │ │ └── inputFiles.lst │ └── testCompile │ └── default-testCompile │ ├── createdFiles.lst │ └── inputFiles.lst ├── surefire-reports │ ├── HelloWorld.AppTest.txt │ └── TEST-HelloWorld.AppTest.xml └── test-classes
Java
1package HelloWorld; 2//package org.jsoup.examples; 3 4import org.jsoup.Jsoup; 5import org.jsoup.helper.Validate; 6import org.jsoup.nodes.Document; 7import org.jsoup.nodes.Element; 8import org.jsoup.select.Elements; 9 10import java.io.IOException; 11 12/** 13 * Example program to list links from a URL. 14 */ 15public class HelloWorld { 16 public static void main(String[] args) throws IOException { 17 Validate.isTrue(args.length == 1, "usage: supply url to fetch"); 18 String url = args[0]; 19 print("Fetching %s...", url); 20 21 Document doc = Jsoup.connect(url).get(); 22 Elements links = doc.select("a[href]"); 23 Elements media = doc.select("[src]"); 24 Elements imports = doc.select("link[href]"); 25 26 print("\nMedia: (%d)", media.size()); 27 for (Element src : media) { 28 if (src.normalName().equals("img")) 29 print(" * %s: <%s> %sx%s (%s)", 30 src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"), 31 trim(src.attr("alt"), 20)); 32 else 33 print(" * %s: <%s>", src.tagName(), src.attr("abs:src")); 34 } 35 36 print("\nImports: (%d)", imports.size()); 37 for (Element link : imports) { 38 print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel")); 39 } 40 41 print("\nLinks: (%d)", links.size()); 42 for (Element link : links) { 43 print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35)); 44 } 45 } 46 47 private static void print(String msg, Object... args) { 48 System.out.println(String.format(msg, args)); 49 } 50 51 private static String trim(String s, int width) { 52 if (s.length() > width) 53 return s.substring(0, width-1) + "."; 54 else 55 return s; 56 } 57}
pom.xml
1<?xml version="1.0" encoding="UTF-8"?> 2 3<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>HelloWorld</groupId> 8 <artifactId>HelloWorld</artifactId> 9 <version>1.0</version> 10 11 <name>HelloWorld</name> 12 <!-- FIXME change it to the project's website --> 13 <url>http://www.example.com</url> 14 15 <properties> 16 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 17 <maven.compiler.source>1.7</maven.compiler.source> 18 <maven.compiler.target>1.7</maven.compiler.target> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>junit</groupId> 24 <artifactId>junit</artifactId> 25 <version>4.11</version> 26 <scope>test</scope> 27 </dependency> 28 <dependency> 29 <!-- jsoup HTML parser library @ https://jsoup.org/ --> 30 <groupId>org.jsoup</groupId> 31 <artifactId>jsoup</artifactId> 32 <version>1.15.1</version> 33 </dependency> 34 </dependencies> 35 36 37 38 <build> 39 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 40 <plugins> 41 <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> 42 <plugin> 43 <artifactId>maven-clean-plugin</artifactId> 44 <version>3.1.0</version> 45 </plugin> 46 <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> 47 <plugin> 48 <artifactId>maven-resources-plugin</artifactId> 49 <version>3.0.2</version> 50 </plugin> 51 <plugin> 52 <artifactId>maven-compiler-plugin</artifactId> 53 <version>3.8.0</version> 54 </plugin> 55 <plugin> 56 <artifactId>maven-surefire-plugin</artifactId> 57 <version>2.22.1</version> 58 </plugin> 59 <plugin> 60 <groupId>org.apache.maven.plugins</groupId> 61 <artifactId>maven-jar-plugin</artifactId> 62 <version>3.0.2</version> 63 <configuration> 64 <archive> 65 <manifest> 66 <mainClass>HelloWorld.HelloWorld</mainClass> 67 <addClasspath>true</addClasspath> 68 <addExtensions>true</addExtensions> 69 <packageName>XXXXXXXX</packageName> 70 </manifest> 71 </archive> 72 </configuration> 73 </plugin> 74 <plugin> 75 <artifactId>maven-install-plugin</artifactId> 76 <version>2.5.2</version> 77 </plugin> 78 <plugin> 79 <artifactId>maven-deploy-plugin</artifactId> 80 <version>2.8.2</version> 81 </plugin> 82 <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> 83 <plugin> 84 <artifactId>maven-site-plugin</artifactId> 85 <version>3.7.1</version> 86 </plugin> 87 <plugin> 88 <artifactId>maven-project-info-reports-plugin</artifactId> 89 <version>3.0.0</version> 90 </plugin> 91 <plugin> 92 <groupId>org.codehaus.mojo</groupId> 93 <artifactId>build-helper-maven-plugin</artifactId> 94 <version>3.3.0</version> 95 <executions> 96 <execution> 97 <id>add-source</id> 98 <phase>generate-sources</phase> 99 <goals> 100 <goal>add-source</goal> 101 </goals> 102 <configuration> 103 <sources> 104 <source>some directory</source> 105 ... 106 </sources> 107 </configuration> 108 </execution> 109 </executions> 110 </plugin> 111 </plugins> 112 </pluginManagement> 113 </build> 114</project> 115
***@MacBook HelloWorld % mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< HelloWorld:HelloWorld >------------------------
[INFO] Building HelloWorld 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ HelloWorld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users//java/HelloWorld/HelloWorld/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ HelloWorld ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ HelloWorld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users//java/HelloWorld/HelloWorld/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ HelloWorld ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ HelloWorld ---
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ HelloWorld ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.411 s
[INFO] Finished at: 2022-06-02T10:06:59+09:00
[INFO] ------------------------------------------------------------------------
***@MacBook HelloWorld % java -jar target/HelloWorld-1.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/helper/Validate
at HelloWorld.HelloWorld.main(HelloWorld.java:38)
Caused by: java.lang.ClassNotFoundException: org.jsoup.helper.Validate
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。