0

javaとmavenの超初心者です。
Mavenを利用してapache-tikaの簡単なサンプルコードを実行させようとしましたが
以下のようなエラーとなりました

> java -jar target\tika-app-1.0-SNAPSHOT.jar
エラー: メイン・クラスne.katch.Appを初期化できません
原因: java.lang.NoClassDefFoundError: org/apache/tika/exception/TikaException

どこかで初歩的なミスを犯しているのだとと思います。ご指摘をいただけたらと思います。

環境

C:\>Users\yasu_>mvn -v
Apache Maven 3.6.3 (cecedd中略883f)
Maven home: C:\maven\bin\..
Java version: 13.0.1, vendor: Oracle Corporation, runtime: C:\jdk-13.0.1
Default locale: ja_JP, platform encoding: MS932
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

Mavenプロジェクトの生成

C:\>Users\yasu_>mvn archetype:generate
(後略)

C:\>Users\yasu_>mvn validate
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< ne.katch:tika-app >--------------------------
[INFO] Building tika-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.156 s
[INFO] Finished at: 2020-05-09T18:10:01+09:00
[INFO] ------------------------------------------------------------------------

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>ne.katch</groupId>
  <artifactId>tika-app</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>tika-app</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.tika</groupId>
      <artifactId>tika-core</artifactId>
      <version>1.24.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tika</groupId>
      <artifactId>tika-parsers</artifactId>
      <version>1.24.1</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <configuration>
           <archive>
              <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>ne.katch.App</mainClass>
              </manifest>
            </archive>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

サンプルコード

package ne.katch;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;

public class App 
{
    public static void main( String[] args )
    {
        try {
            Tika tika = new Tika();
            System.out.println(tika.parseToString(new File("C:\\sample.pdf")));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TikaException e) {
            e.printStackTrace();
        }
    }
}

mavenでのビルド

c:\Users\yasu_\tika-app>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< ne.katch:tika-app >--------------------------
[INFO] Building tika-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ tika-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory c:\Users\yasu_\tika-app\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ tika-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to c:\Users\yasu_\tika-app\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ tika-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory c:\Users\yasu_\tika-app\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ tika-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to c:\Users\yasu_\tika-app\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ tika-app ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running ne.katch.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 s - in ne.katch.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ tika-app ---
[INFO] Building jar: c:\Users\yasu_\tika-app\target\tika-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.051 s
[INFO] Finished at: 2020-05-09T18:27:04+09:00
[INFO] ------------------------------------------------------------------------

jarファイルの実行

c:\Users\yasu_\tika-app>java -jar target\tika-app-1.0-SNAPSHOT.jar
エラー: メイン・クラスne.katch.Appを初期化できません
原因: java.lang.NoClassDefFoundError: org/apache/tika/exception/TikaException

1 件の回答 1

0

依存ライブラリーも含めて実行可能jarをつくりたいということであれば、Maven Assembly Pluginなどを使う必要がありますね。

このページがなんかが分かりやすいですかね。
https://www.shookuro.com/entry/2018/03/03/172556

【参考】Apache Maven Assembly Plugin
https://maven.apache.org/plugins/maven-assembly-plugin/index.html

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。