如何解决Netbeans第三方库上的java.lang.module.InvalidModuleDescriptorException?

时间:2019-06-05 04:30:00

标签: java apache-tika java-11 openjfx netbeans-11

我正在将一个正常运行的Java 8项目转换为Java11。我还不想使用模块(我知道有一些优点,但是到目前为止,我只希望该项目在Java 11下运行) 。因此,项目src文件夹中没有module-info.java

我使用Netbeans 11,并且可以成功构建(基于Maven的)项目(在Linux下使用openjdk 11)。但是,当我运行它时,我得到

  

启动层初始化期间发生错误   java.lang.module.FindException:无法导出/home/helloWorld/.m2/repository/org/apache/tika/tika-parsers/1.21/tika-parsers-1.21.jar的模块描述符   原因:java.lang.module.InvalidModuleDescriptorException:提供程序类org.apache.tika.parser.external.CompositeExternalParser不在模块中

我的课程都在软件包中,因此answer不适合,other one也不合适。

项目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>MRE</groupId>
<artifactId>Example</artifactId>
<version>1.0.12</version>
<packaging>jar</packaging>

<parent>
    <artifactId>MREWeb</artifactId>
    <groupId>MRE</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

<dependencies>
    <dependency>            
        <groupId>MRE</groupId>
        <artifactId>Base</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tika</groupId>
        <artifactId>tika-parsers</artifactId>
        <version>1.21</version>
        <type>jar</type>
    </dependency>        

    <dependency>
        <groupId>org.apache.opennlp</groupId>
        <artifactId>opennlp-tools</artifactId>
        <version>1.9.0</version>
    </dependency>

    <dependency>
        <groupId>net.sourceforge</groupId>
        <artifactId>prajna</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>com.github.stefanbirkner</groupId>
        <artifactId>system-rules</artifactId>
        <version>1.19.0</version>
        <scope>test</scope>
    </dependency>        

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>11.0.2</version>
        <type>jar</type>
    </dependency>

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11.0.2</version>
    </dependency>        

<build>

    <resources>
        <resource>
            <!--We specify the resource folder that should be included-->
            <directory>src/main/resources</directory>              
        </resource>
    </resources>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>3.0.0-M2</version>
            <executions>
                <execution>
                    <id>enforce-maven</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <requireMavenVersion>
                                <version>3.3.9</version>
                            </requireMavenVersion>
                        </rules>    
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!--See here for details https://www.baeldung.com/executable-jar-with-maven-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>
                                    ${mainClass}
                                </mainClass>                                    
                            </manifest>
                            <!--Adds custom entry for version to manifest and changes Built By value-->
                            <manifestEntries>
                                <!--We use implementation version so that it can be retrieved within java from package methods-->
                                <Implementation-Version>${project.version}</Implementation-Version>
                                <Implementation-Title>${project.artifactId}</Implementation-Title>
                                <Built-By>HelloWorld</Built-By>
                            </manifestEntries>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.2</version>
            <configuration>
                <mainClass>${mainClass}</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>11</release>                    
            </configuration>
        </plugin>           

    </plugins>

</build>

<name>MREWeb_Example</name>
</project>

Netbeans在其控制台中输出的命令行为:

  

cd / home / helloWorld / java / projects / example;   JAVA_HOME = / usr / lib / jvm / java-11-openjdk-amd64   / home / helloworld / netbeans / java / maven / bin / mvn“ -Dexec.args = -classpath   %classpath mre.example.MainApp“   -Dexec.executable = / usr / lib / jvm / java-11-openjdk-amd64 / bin / java -DskipTests = true干净的javafx:run

如何将CompositeExternalParser类放入模块(实际上是哪个模块)?

编辑(按照注释的建议将javafx添加到模块路径和添加模块之后)

如果我通过java --module-path /home/helloworld/Java/JavaFx/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml -jar example-jar-with-dependencies.jar从命令行运行已编译的(胖)jar,则该应用程序运行良好。因此,在Netbeans中宁可做些事,对(但是;-)是什么)?

最小可复制示例(在评论中要求)

  1. 通过Maven原型(javafx-archetype-simple)创建一个新的Maven项目,并遵循OpenJfX tutorial for non modular maven project in Netbeans
  2. 在pom.xml中添加Tika解析器依赖项
  <dependency>
        <groupId>org.apache.tika</groupId>
        <artifactId>tika-parsers</artifactId>
        <version>1.21</version>
        <type>jar</type>
   </dependency>

pom.xml变为

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>minimum.reproducible</groupId>
    <artifactId>MavenExample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
        </dependency>
                <dependency>
            <groupId>org.apache.tika</groupId>
            <artifactId>tika-parsers</artifactId>
            <version>1.21</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.2</version>
                 <configuration>
                       <mainClass>minimum.reproducible.mavenexample.App</mainClass>
                 </configuration>
             </plugin>
 <!--To include the main class attribute in the manifest file-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>minimum.reproducible.mavenexample.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
         </plugins>
     </build>
 </project>
  1. 添加此方法from Tika example
public String parseExample() throws IOException, SAXException, TikaException {
    AutoDetectParser parser = new AutoDetectParser();
    BodyContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    try (InputStream stream = new FileInputStream(new File("/home/helloWorld/Texts/test.txt"))) {
        parser.parse(stream, handler, metadata);
        return handler.toString();
    }
  1. 使用一些代码扩展start方法以运行先前创建的方法
    public void start(Stage stage) {
        var javaVersion = SystemInfo.javaVersion();
        var javafxVersion = SystemInfo.javafxVersion();

        var label = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
        var scene = new Scene(new StackPane(label), 640, 480);
        stage.setScene(scene);
        stage.show();

        System.err.println("Starting tika parsing");
        try {
            String result = parseExample();
            System.err.println("The parsing yielded : ");
            System.err.println(result);

        } catch (IOException | SAXException | TikaException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE,
                    null,
                    ex);
        }
    }
  1. 现在清理并构建并运行项目。描述的错误应该发生。

  2. 请注意:如果从Pom中没有的命令行和App.java中与Tika相关的东西启动此最小可重现的示例程序,则该程序运行良好。

java --module-path /home/helloworld/Java/JavaFx/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml -jar MavenExample-1.0-SNAPSHOT.jar

但是,如果我尝试再次从命令行运行它,但是如上面最小可复制示例中所述,使用了所有Tika的东西,我会得到

  

缺少JavaFX应用程序类minimum.reproducible.mavenexample.App

尽管Netbeans 11一直显示上面提到的java.lang.module.InvalidModuleDescriptorException。

  1. 但是,是否将以下几行添加到pom.xml
<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <mainClass>minimum.reproducible.mavenexample.App</mainClass>
                </configuration>
            </plugin>

现在可以通过mvn exec:java运行该项目。所以我想问题出在javafx-maven-plugin,不是吗?

任何帮助表示赞赏;-)

0 个答案:

没有答案