java.lang.NoClassDefFoundError 在使用 maven eclipse AsciidoctorJ 时

java.lang.NoClassDefFoundError while using maven eclipse AsciidoctorJ

我正在尝试使用 Asciidoctor 使用 asciidocj (convertFile) 中内置的方法生成一个 html 文件 我得到 java.lang.NoClassDefFoundError:org/asciidoctor/OptionsBuilder

我正在研究 eclipse;行家和 我在 pom.xml 文件中添加了 Asciidoctor 依赖项。

public void view(String document) {
        OptionsBuilder op = OptionsBuilder.options().toFile(false);

        Asciidoctor asciidoctor = create();

        String html = asciidoctor.convertFile(new File(this.path + "/" + document + ".adoc"), op.asMap());
        System.out.println(html);
    }
<?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>ProgGl</groupId>
   <artifactId>PriseDeNotes</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>PriseDeNotes</name>
   <description>Projet de Prog Gl</description>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
      <main-class>ProgGl.PriseDeNote.App</main-class>
   </properties>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
               <archive>
                  <manifest>
                     <mainClass>${main-class}</mainClass>
                  </manifest>
               </archive>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.asciidoctor</groupId>
            <artifactId>asciidoctor-maven-plugin</artifactId>
            <version>1.5.6</version>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.3</version>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-project-info-reports-plugin</artifactId>
            <version>2.7</version>
         </plugin>
         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
               <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
               </descriptorRefs>
               <archive>
                  <manifest>
                     <mainClass>${main-class}</mainClass>
                  </manifest>
               </archive>
            </configuration>
            <executions>
               <execution>
                  <id>make-assembly</id>
                  <phase>package</phase>
                  <goals>
                     <goal>single</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
               <execution>
                  <id>attach-sources</id>
                  <phase>package</phase>
                  <goals>
                     <goal>jar-no-fork</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>3.0.0-M1</version>
            <executions>
               <execution>
                  <id>attach-javadocs</id>
                  <phase>package</phase>
                  <goals>
                     <goal>jar</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <groupId>com.github.spotbugs</groupId>
            <artifactId>spotbugs-maven-plugin</artifactId>
            <version>3.1.7</version>
            <dependencies>
               <!-- overwrite dependency on spotbugs if you want to specify the version of spotbugs -->
               <dependency>
                  <groupId>com.github.spotbugs</groupId>
                  <artifactId>spotbugs</artifactId>
                  <version>3.1.8</version>
               </dependency>
            </dependencies>
         </plugin>
      </plugins>
   </build>
   <reporting>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>3.0.0</version>
            <reportSets>
               <reportSet>
                  <reports>
                     <report>checkstyle</report>
                  </reports>
               </reportSet>
            </reportSets>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jxr-plugin</artifactId>
            <version>2.3</version>
         </plugin>
      </plugins>
   </reporting>
   <dependencies>
        <dependency>
            <groupId>org.asciidoctor</groupId>
            <artifactId>asciidoctorj</artifactId>
            <version>2.0.0</version>
        </dependency>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.hamcrest</groupId>
         <artifactId>hamcrest-all</artifactId>
         <version>1.3</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>

mvn 安装,打包一切正常, 当我执行程序时出现此错误。 我读到它是关于 class 路径等...但我不知道该怎么做

Exception in thread "main" java.lang.NoClassDefFoundError: org/asciidoctor/OptionsBuilder
    at ProgGl.PriseDeNote.Application.view(Application.java:171)
    at ProgGl.PriseDeNote.Commands.ViewCommand.execute(ViewCommand.java:22)
    at ProgGl.PriseDeNote.App.main(App.java:66)
Caused by: java.lang.ClassNotFoundException: org.asciidoctor.OptionsBuilder
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more


谢谢。

一般情况下,maven 程序集插件会生成另一个 .jar,称为 target/PriseDeNotes-0.0.1-SNAPSHOT-jar-with-dependencies.jar。 您可以尝试 java -jar target/PriseDeNotes-0.0.1-SNAPSHOT-jar-with-dependencies.jar 到 运行 应用程序。

我认为问题在于,maven 没有将依赖项复制到 jar 中。尝试使用相关插件复制目标中的依赖项,您可以在此处找到:

https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html

有了这个,它应该将所需的依赖项复制到目标文件夹。

我看到你已经 maven-assembly-plugin 了。使用 mvn clean compile assembly:single 编译,然后使用 java -jar target/PriseDeNotes-0.0.1-SNAPSHOT-jar-with-dependencies.jar.

编译 运行