0

我正在将工作中的 java 8 项目转换为 java 11。我还不想使用模块(我知道有一些优点,但到目前为止我只想让项目在 java 11 下运行)。所以module-info.java项目src文件夹中没有。

我使用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 不在模块中

我的课程都在包中,所以这个答案不适合,另一个也不适合。

该项目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 clean javafx:ru​​n

如何将CompositeExternalParser课程放入模块中(确实是哪个模块)?

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

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

最小可重现示例(如评论中要求的那样)

  1. 通过 Maven Archetype (javafx-archetype-simple) 创建一个新的 Maven 项目,并按照OpenJfX 教程进行 Netbeans 中的非模块化 maven 项目
  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. 从 Tika 示例中添加此方法
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 中的 Tika 和 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,不是吗?

任何帮助表示赞赏;-)

4

0 回答 0