通过模块路径在 jdk16 上使用 Ant 构建 jar

问题描述

我正在开始从 Java 8 迁移到 Java 16。 我正在尝试使用 ModulePath 而不是 Classpath。 为了测试迁移,我在 com.test.main 中创建了一个只有一个类 (Main.java) 的简单新项目。 我也需要 javafx 库。我的 module-info.java 进行导入:

open module com.test.main {
requires java.desktop;
requires javafx.graphics;
requires javafx.swing;
requires javafx.fxml;
requires javafx.controls;
}

你可以看到我的项目目录here

我使用 ant 来构建我的项目,所以我尝试调整它。

<project name="Test" basedir=".">
<description>
    build the test project
</description>

<!-- set global properties for this build -->
<property name="app.name"       value="Test" />
<property name="VERSION"        value="1.0" />
<property name="RELEASE"        value="0" />
<property name="debug"          value="false" />
<property name="optimize"       value="true" />
<property name="deprecation"    value="true" />

<property name="src"            location=".."/>
<property name="build"          location="../bin"/>
<property name="classdir"       location="../bin/classes"/>


<property name="dist"           location="../jlib"/>
<property name="package.dir"    location="../packaging"/>
<property name="images_dist"    location="../bin/classes/images"/>


<property name="external_components.location" value="../external" />

<!-- JavaFx -->
<path id="javafx.path">
    <fileset dir="${external_components.location}/javafx-sdk-16">
        <!-- catch all jar files in the current directory -->
        <include name="**/*.jar" />
    </fileset>
</path>
<!-- convert path to property -->
<pathconvert property="javaFxPath" refid="javafx.path" />

<!-- ModulePath -->
<path id="module.path">
    <pathelement path="${javaFxPath}" />
</path>
<!-- convert path to property -->
<pathconvert property="modulePath" refid="module.path" />

<!-- general Init-->
<target name="init" depends="clean">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
    <mkdir dir="${classdir}"/>
    <mkdir dir="${dist}"/>
</target>

<!-- Compilation -->
<target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac includeantruntime="false" destdir="${classdir}" fork="yes" verbose="true" debug="${debug}" optimize="${optimize}" deprecation="${deprecation}" modulepath="${modulePath}" source="16">
        <src path="${src}"/>
    </javac>
</target>

<!-- Production of the JAR file -->
<target name="dist" depends="compile"
        description="generate the distribution" >

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/${app.name}.jar" basedir="${classdir}" >
        <manifest>
            <attribute name="Built-By" value="${user.name}"/>
            <attribute name="Main-Class" value="Main"/>
        </manifest>
    </jar>
</target>

<!-- CLEAN -->
<target name="clean" description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete verbose="true" dir="${classdir}"/>
    <delete verbose="true" dir="${build}"/>
</target>

当我运行它时,一切都很好,Ant 构建正确完成。 但是,当我尝试使用以下命令运行我的 jar 时:

java -jar .\jlib\Test.jar com.test.main.Main

我收到此错误

Error: Could not find or load main class Main
Caused by: java.lang.classNotFoundException: Main

有人知道我做错了什么吗?谢谢你的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)